| 序号 | 方法 | 描述 | 示例 |
|---|---|---|---|
| 1 | str.super() | 将输入的字母全部大写 | print('HEllO PYthon'.upper()) |
| 2 | str.lower () | 将输入的字母全部小写 | print('HEllO PYthon'.lower()) |
| 3 | str.swapcase() | 将输入的字母大小写互换 | print('HEllO PYthon'.swapcase()) |
| 4 | str.capitalize() | 首字母大写,其余小写 | print('hellO PYthon'.capitalize()) |
| 5 | str.title() | 首字母大写 | print('hellO pYthon'.title()) |
| 6 | str.ljust(width[, fillchar]) | 规定固定长度,左对齐,多余位置自定义字符填充,默认空格 | print('hello'.ljust(11,'=')) |
| 7 | str.center(width[, fillchar]) | 规定固定长度,居中对齐,多余位置自定义字符填充,默认空格 | print('hello'.center(11,'=')) |
| 8 | str.rjust(width[, fillchar]) | 规定固定长度,右对齐,多余位置自定义字符填充,默认空格 | print('hello'.rjust(11,'=')) |
| 9 | find(str, beg=0 end=len(string)) | 搜索指定字符串,有返回第一次出现的索引号,没有返回-1 | print('hello'.find('l')) |
| 10 | rfind(str, beg=0,end=len(string)) | 从右往左搜索指定字符,有返回第一次出现的索引号,没有返回-1 | print('hello'.find('l')) |
| 11 | index(str, beg=0, end=len(string)) | 从右往左搜索指定字符串,有返回第一次出现的索引号,没有则显示错误 | print('hello'.index('n')) |
| 12 | rindex(str, beg=0, end=len(string)) | 搜索指定字符串,有返回第一次出现的索引号,没有则显示错误 | print('hello'.index('n')) |
| 13 | replace(old, new [, max]) | 将old字符串替换为new字符串 | print('helo'.replace('l','ll')) |
| 14 | lstrip([chars]) | 去除字符串左边的空格 | print(' hello '.strip()) |
| 15 | strip([chars]) | 去除字符串两边的空格 | print(' hello '.strip()) |
| 16 | rstrip([chars]) | 去除字符串右边的空格 | print(' hello '.strip()) |
| 17 | split(str="", num=string.count(str)) | 按照指定字符分割字符串 | print('h%e%l%l%o'.split('%')) |
| 18 | startswith(str, beg=0,end=len(string)) | 检查是否是str字符串开头,是则True,否则False | print('hello python'.startswith('hello')) |
| 19 | endswith(str, beg=0,end=len(string)) | 检查是否是str字符串结尾,是则True,否则False | print('hello python'.endswith('python')) |
| 20 | isalnum() | 判断字符串是否全是数字或字母,是则True,否则False | print('hello python'.isalnum()) |
| 21 | isalpha() | 判断是否全是字母,是则True,否则False | print('hello python'.isalpha()) |
| 22 | isdigit() | 判断是否全是数字,是则True,否则False | print('123'.isdigit()) |
| 23 | isnumeric() | 判断是否全是数字字符,是则True,否则False | print('123'.isnumeric()) |
| 24 | islower() | 判断字母是否全是小写,是则True,否则False | print('Hello python'.islower()) |
| 25 | isupper() | 判断字母是否全是大写,是则True,否则False | print('HELLO PYTHON1'.isupper()) |
| 26 | istitle() | 判断首字母是否都是大写,是则True,否则False | print('Hello Python'.istitle()) |
| 27 | isspace() | 判断字符是否是空格,是则True,否则False | print(' '.isspace()) |
| 28 | join(seq) | 以指定字符合并字符串 | print('#'.join('hello')) |
| 29 | len(string) | 获取字符串长度 | print(len('hello python')) |
| 30 | count(str) | 获取子字符串在字符串中出现的次数 | print('hello python'.count('l')) |
| 31 | expandtabs(tabsize=8) | 把字符串中的tab符号转换为空格,默认为8 | print('hello\tpython'.expandtabs(1)) |
| 32 | max(str) | 返回字符串中最大的字母 | print(max('hello python')) |
| 33 | min(str) | 返回字符串中最小的字母 | print(min('hello python')) |
| 34 | zfill(width) | 返回字符串为width长度的字符串,原字符串向右对齐 | print('hello python'.zfill(20)) |
| 35 | isdecimal() | 判断字符串是否只包含十进制字符,是则True,否则False | print('123'.isdecimal()) |
作业day4-字符串相关函数
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
相关阅读更多精彩内容
- 一、获取字符 1.获取单个字符 字符串中的每一个字符都会对应一个唯一的下标(索引)用来表示字符在字符串中的位置.下...
- 1.getchar --获取字符 1.获取单个字符 字符串中的每一个字符都会对应一个唯一的下标(索引)用来表示字...