美文网首页
作业day4-字符串相关函数

作业day4-字符串相关函数

作者: RiskHY | 来源:发表于2018-12-27 21:00 被阅读0次
序号 方法 描述 示例
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-字符串相关函数

      本文链接:https://www.haomeiwen.com/subject/clvdlqtx.html