Python 内置函数收集
1.python3支持Unicode
使用ord()可以把字符转化成对应的Unicode码
使用chr()可以把数字转化为字符串
2.字符替换操作
a = ‘asdfasdfadf’
a
‘asdfasdfadf’a.replace(‘a’,‘f’)
‘fsdffsdffdf’a
‘asdfasdfadf’a = a.replace(‘a’,‘f’)
a
‘fsdffsdffdf’
3.字符串切片操作
a = “abcdefghijklmn”
a
‘abcdefghijklmn’a[::]
‘abcdefghijklmn’a[1:2:3]
‘b’a[0:7:3]
‘adg’a[::-1]
‘nmlkjihgfedcba’
4.字符串分割split()合并join()
a = “to b or not to b”
a.split()
[‘to’, ‘b’, ‘or’, ‘not’, ‘to’, ‘b’]a.split(“b”)
['to ', ’ or not to ', ‘’]
a = [‘to’,‘be or not’,‘to be’]
a
[‘to’, ‘be or not’, ‘to be’]"".join(a)
'tobe or not*to be’“”.join(a)
‘tobe or notto be’
join的运行时间远小于+=
5.字符串驻留机制
只有符合标识符规则的字符串(仅包含下划线、字母和数字)会启用驻留机制
b = “3#”
c = “3#”
b is c
Falseb = “3_”
c = “3_”
b is c
True
6.成员操作符 in、no int
判断字符是否包含在字符串中
7.常用查找
a = “abcdefghijklmn”
len(a)
14a.startswith(“c”)
Falsea.startswith(“a”)
Truea.endswith(“n”)
Truea.find(“e”)
4a.rfind(“e”)
4a.count(“c”)
1a.isalnum()
True
8.去除首尾信息
a = “ryt”
a.strip(’’)
'ry*t’a.lstrip(’’)
'ryt’a.rstrip(’’)
'ryt’
9.大小写转换
a = “Ren you Tong”
a.capitalize()
‘Ren you tong’a.title()
‘Ren You Tong’a.upper()
‘REN YOU TONG’a.lower()
‘ren you tong’a.swapcase()
‘rEN YOU tONG’
10.格式排版
center() ljust() rjust()
11.else
isalnum
isalpha
isalspace
isupper
islower
12.format()
a = “name is {name}, age is {age}”
b = a.format(name = “ren youtong”, age = 24)
b
‘name is ren youtong, age is 24’
“name is {0}, age is {1:*^8}”.format(“Renyoutong”,24)
‘name is Renyoutong, age is 24’
版权归原作者 哀如冷月 所有, 如有侵权,请联系我们删除。