1.len()
len() 函数计算并返回字符串元素个数。(格式:len(string) )(string 代表字符串名字)
>>> S = 'abc'
>>> S
'abc'
>>> len(S)
3
2.max()、min()
max()、min()函数分别返回最大值和最小值,数字就返回数字,字母根据ASCII计算。(格式:max(string)、min(string) )
>>> S = 'abc'
>>> S
'abc'
>>> max(S)
'c'
>>> min(S)
'a'
>>> S = '123'
>>> S
'123'
>>> max(S)
'3'
>>> min(S)
'1'
3.enumerate()
enumerate() 函数用来遍历序列的坐标和元素。(格式:enumerate(string) )
>>> S = 'abcd'
>>> S
'abcd'
>>> for i,s in enumerate(S):
print(i,s)
0 a
1 b
2 c
3 d
单纯返回的话不知道是什么东西。。。。
>>> S = 'abc'
>>> S
'abc'
>>> enumerate(S)
<enumerate object at 0x0000000003119168>
4.str()
str() 函数将参数转变为字符串类型并返回副本,不改变原有类型。(格式:str(string) )
>>> T = [1,2,3]
>>> T
[1, 2, 3]
>>> type(T)
<class 'list'>
>>> str(T)
'[1, 2, 3]'
>>> type(T)
<class 'list'>
>>> type(str(T))
<class 'str'>
5.chr() 、 ord()
chr() 函数可将传入的单个数字转化成对应的ASCII的字母返回。
ord() 函数可将传入的单个字母转化成对应的ASCII的数字返回。
>>> chr(65)
'A'
>>> ord('a')
97
>>> ord('abc')<span style="white-space:pre"> </span>#传入多个字母出现异常
Traceback (most recent call last):
File "<pyshell#383>", line 1, in <module>
ord('abc')
TypeError: ord() expected a character, but string of length 3 found
capitalize() 函数将字符串的第一个字母大写并返回副本,不改变原有数据。(格式:string.capitalize())
>>> S = 'abc'
>>> S
'abc'
>>> S.capitalize()
'Abc'
>>> S
'abc'
7.center()
center() 函数将使字符串居中输出,两边用空格填充,不改变原有数据。(格式:string.center(width) )(width为参数,表示总宽度 )
>>> S = 'abc'
>>> S
'abc'
>>> S.center(10)
' abc ' #宽度10
>>> S
'abc'
>>> S.center(2)
'abc' #若设置的宽度小于元字符串,没什么变化。
>>> S
'abc'
8.count()
c