1 # r开头的函数是倒序
2 s =
'Helllo, WOr\tlld'
3
4 # 是否包含
5 print(s.
__contains__(
'he'),
'he' in s)
6
7 # 首字母大写
8 print(s.capitalize(),
'capitalize')
9
10 # 大写变小写
11 print(s.casefold(),
'casefold')
12
13 # 按指定长度,左右添加字符,居中文本
14 print(s.center(50,
'-'),
'center')
15
16 # 某个字符出现的次数,区分大小写,后面两个参数是起始位置,和结束位置
17 print(s.count(
'l', 7),
'count')
18
19 # 判断是否以指定字符结尾,后两个参数规定开始位置和结束位置
20 print(s.startswith(
'H'),
'startswith')
21
22 # 判断是否以指定字符结尾,后两个参数规定开始位置和结束位置
23 print(s.endswith(
'lo', 2, 5),
'endswith')
24
25 # 将tab转换成指定长度空格,一般tab是4个空格,默认是转成8个
26 print(s.expandtabs(10),
'expandtabs')
27
28 # 查找某字符首次出现的位置,后两个参数可以指定开始位置和结束位置(倒找是rfind)
29 print(s.find(
'l'),
'find')
30
31 # 递归搜索指定字符所有出现位置
32 def find_all(self, sub, start, ret=
[]):
33 seat =
self.find(sub, start)
34 if seat != -1:
# 未找到返回-1
35 ret.append(seat)
# 加入有效值
36 # 下次从当前位置+字符长度开始搜索(也可以从+1开始,那么'Helll0',就有两个'll')
37 start = seat + sub.
__len__()
38 find_all(self, sub, start)
# 递归函数
39 return ret
40 print(find_all(s,
'll', 0),
'find_all')
41
42 # 和find使用方法一样,但是找不到的话会报错
43 print(s.index(
'e'),
'index')
44
45 # 字符串格式化,和'1234 %s %s' % (变量,变量)使用方式一样
46 b =
'hello {0}{1} ...'
47 print(b.format(
'wo',
'rld'),
'format')
48
49 # 是否是纯字母+数字
50 print(
's11'.isalnum(),
'isalnum')
51
52 # 是否纯字母
53 print(
'ssdd'.isalpha(),
'isalpha')
54
55 # 是否十进制整数
56 print(
'10.1'.isdecimal(),
'isdecimal')
57
58 # 是否纯数字
59 print(
'12344'.isdigit(),
'isdigit')
60
61 # 判断字符是否合法,可用来判断变量名
62 print(
'adD'.isidentifier(),
'isidentifier')
63
64 # 字母是否纯小写
65 print(
'sdff345'.islower(),
'islower')
66
67 # 是否是数字
68 print(
'12340.1'.isnumeric(),
'isnumeric')
69
70 # 是否可打印
71 print(
'234'.isprintable(),
'isprintable')
72
73 # 是否为纯空格
74 print(
' '.isspace(),
'isspace')
75
76 # 首字母是否大写
77 print(
'Tsdlkj'.istitle(),
'istitle')
78
79 # 单词首字母大写
80 print(
'sfiu,fuio'.title(),
'title')
81
82 # 字母是否纯大写
83 print(
'234W'.isupper(),
'isupper')
84
85 # 在指定字符串或序列迭代器(两个以上)中间依次加入原始文本进行连接,最后以指定字符串尾的字符结尾
86 print(
'--'.join(
'123456'),
'join')
87
88 # 和center使用一样,这个是居左
89 print(
'1234'.ljust(20,
'*'),
'ljust')
90
91 # 字符居右
92 print(
'1234'.rjust(20,
'*'),
'rjust')
93
94 # 大写转小写
95 print(
'sTeetWE'.lower(),
'lower')
96
97 # 删除左右空
98 print(
' abc '.strip(),
'strip')
99
100 # 删除左空
101 print(
' abc '.lstrip(),
'lstrip')
102
103 # 删除右空
104 print(
' abc '.rstrip(),
'rstrip')
105
106 # maketrans和translate 对应使用
107 inttab =
"cbade" # 被替换的字符
108 outtab =
'12345' # 用作替换的字符,替换inttab对应位置(1替换c,2替换b....依次类推)
109 table = str.maketrans(inttab, outtab)
# 生成替换表
110 s =
'abcde frtuatodtieus abcde'
111 print(s.translate(table),
'translate')
# 开始替换
112
113 # 文本分割,用作分割的字符不会保存
114 print(
'hello'.split(
'l'),
'split')
115
116 # 按行分割,同 split('\n')
117 print(
'123\n456\n789'.splitlines(),
'splitlines')
118
119 # 文本分割,用作分割的字符会保存
120 print(
'hello'.partition(
'l'),
'partition')
121
122
123 # 文本替换,第三个参数指定替换次数,默认全部替换
124 print(
'1234'.replace(
'2',
'45'),
'replace')
125
126 # 大小写互转
127 print(
'WdftjWituHLj'.swapcase(),
'swapcase')
128
129 # 在左侧用0补齐宽度
130 print(
'1234'.zfill(10),
'zfill')
131
132 exit()
转载于:https://www.cnblogs.com/xh4528/p/6497651.html