Note Python string built-in method

  

A. Case conversion

① capitalize()

capitalize() #字符串首字母大写

>>> str0 = 'hello World' >>> str0.capitalize() 'Hello world' 

②upper(), lower()

upper() #将字符串的所有小写字符转化为大写
lower() #将字符串的所有大写字符转化为小写

>>> str1 = 'Hello World' >>> str1.upper() 'HELLO WORLD' >>> str1.lower() 'hello world' 

③swapcase()

swapcase() #将字符串中的大写字符改为小写,小写字符改为大写

>>> str1 = 'Hello World' >>> str1.swapcase() 'hELLO wORLD' 

II. Content judge

①startswith(),endswith()

startswith() #判断字符串是否以指定参数开始,返回True或False
endswith() #判断字符串是否以指定参数结束,返回True或False

>>> str1 = 'Hello World' >>> str1.startswith('h') False >>> str1.endswith('d') True 

tip: to distinguish between the case when the judgment may be added a second, three parameters to limit the scope.

②isupper(),islower()

isupper() #如果字符串中的字母均为大写返回True,否则返回 False
islower() #如果字符串中的字母均为小写返回True,否则返回 False

>>> str1 = 'Hello World' >>> str2 = '123asdxfgs...' >>> str1.isupper() False >>> str2.islower() True 

tip: string can contain special characters and numbers

③isalpha (), isdigit (), isalnum ()

isalpha() #如果字符串中只包含字母返回 True,否则返回 False
isdigit() #如果字符串中只包含数字返回 True,否则返回 False
isalnum() #如果字符串中只包含字母或数字返回 True,否则返回 False

>>> str3 = '123456789' >>> str2 = '123asdxfgs...' >>> str3.isalpha() False >>> str3.isdigit() True >>> str2.isalnum() False 

III. Content Find

find(),rfind(),index(),rindex()

find() #查找指定参数是否在字符串中,查找成功返回字符下标,失败则返回-1
rfind() #功能和find()一致,但查找方向从右边开始
index() #和find功能一样,但查找失败会产生错误
rindex() #功能和index()一致,但查找方向从右边开始

>>> str2 = '123asdxfgs...' >>> str2.find('123') 0 >>> str2.find('1234') -1 >>> str2.index('x') 6 >>> str2.index('b') Traceback (most recent call last): File "<pyshell#33>", line 1, in <module> str2.index('b') ValueError: substring not found >>> str4 = 'test88ooo88' >>> str4.find('88') 4 >>> str4.rfind('88') 9 

tip: can add a second, three parameters to limit the scope.

IV. Replace, add, delete,

①strip(),lstrip(),rstrip()

strip() #默认删除字符串前后的空格,若加上参数,则改为删除字符串前后的该参数
lstrip() #去掉字符串前面的空格,若加上参数,则改为删除字符串前的该参数
rstrip() #删除字符串末尾的空格,若加上参数,则改为删除字符串后的该参数

>>> str5 = ' content ' >>> str5.lstrip() 'content ' >>> str5.rstrip() ' content' >>> str5.strip() 'content' >>> str5.strip('t') ' content ' >>> str6 = '123and123' >>> str6.strip('123') 'and' >>> str6.lstrip('123') 'and123' 

tip: strip()You can only delete the specified content before and after the string, the string can not be misunderstood as to delete all the characters you want to achieve this function, it is recommended to use replace().

②replace()

replace(old,new[,count]) #将字符串中的所有old用new替换,若使用count参数,则替换次数不超过count次。

>>> str7 = 'xxHxexlxxlxo Wxxxxorld' >>> str7.replace('x', '') 'Hello World' >>> str7.replace('x', 'A') 'AAHAeAlAAlAo WAAAAorld' >>> str7.replace('x', 'A', 1) 'AxHxexlxxlxo Wxxxxorld' 

③format()

format() #格式化字符串

>>> "姓名:{}, 性别:{}".format('张三','男') '姓名:张三, 性别:男' >>> "姓名:{a}, 性别:{b}".format(b='男',a='张三') '姓名:张三, 性别:男' 

tip: often used in conjunction with {}, the number of parameters by themselves.

④join()

join() #以原字符串作为分隔符,插入到参数中每个字符之间。

>>> str_break = 'xx' >>> str_break.join('AB') 'AxxB' >>> str_break.join('ABC') 'AxxBxxC' 

V. segmentation

①split () rsplit ()

split() #以第一个参数为分隔符分割字符串,返回一个列表
rsplit() #和split()功能相同,但是从右边开始分割

>>> str8 = 'AxxBxxC' >>> str8.split('xx') ['A', 'B', 'C'] >>> str8.split('x') ['A', '', 'B', '', 'C'] >>> str8.split('xxx') ['AxxBxxC'] 

tip: one can add a second parameter limit frequency

>>> str8.split('xx', 1) ['A', 'BxxC'] 

②partition(),rpartition()

partition() #将字符串从参数处分成'参数前','参数','参数后'三段,返回一个三元元组
rpartition() #功能和partition()一致,但从右边开始

>>> str8 = 'AxxBxxC' >>> str8.partition('xx') ('A', 'xx', 'BxxC') >>> str8.rpartition('xx') ('AxxB', 'xx', 'C') >>> str8.partition('zzz') ('AxxBxxC', '', '') 

tip: if no parameter is returned('字符串', '', '')

VI. Count

count()

count() #参数为字符串的一个子串,返回该子串出现的次数

>>> str8 = 'AxxBxxC' >>> str8.count('xx') 2 

tip: can add a second, three parameters to limit the scope.

Seven other

center()

center() #将某字符串居中,并以指定字符填充至指定长度。

>>> "test".center(20, '*') '********test********'

Guess you like

Origin www.cnblogs.com/68xi/p/11621959.html