字符串操作:1、 下载python官方手册2、 先定义一个字符串变量 A = ‘abc’ A.两次TAB键 help(A.选项) #查看帮助
>> ' ABC '. Lower () #XXX. Lower converts uppercase strings to lowercase
' ABC '
XXX.title() #将字符串每个单词的首字母转换为大写XXX.capitalize() #将字符串的首字母转换为大写XXX.center(长度, 充填符) #字符串居中XXX.ljust(宽度, 充填符号) #字符串向左对齐XXX.rjust(宽度, 充填符号) #字符串向右对齐XXX.count(‘字符‘) #统计单个字符出现的次数
>> hi = ' Hello world '
>> hi.startswith (' l ') #判断行首是否为L, correct but true, error returned false
False
>> hi.startswith (' h ')
True
>> hi.endswith (' d ') #判断结尾
True
>> a = ' [email protected]#$ '
>> A.islower () # # #XXX. Islower returns True if all lowercase in the string is determined, otherwise false is returned. Ignore numbers, characters
True
>> a = ' [email protected] '
>> A.islower ()
False
>> a = ' [email protected]#%! '
>> A.isupper () #判断字符串中是否全部为大写, yes returns true, otherwise false is returned. Ignore numbers, write characters, or return true, otherwise false. Ignore numbers, characters
True
>> a = ' WGWEGWEGWEWASDG '
>> A.isalpha () #判断是否全部为字母, ignoring case, character, number
True
>> a = ' WGWEGWEGWEWASDG1 '
>> A.isalpha ()
False
>> ' 434 '. IsDigit () #判断字符串是否全部是数字
True
>> ' 434a '. IsDigit ()
False
>> ' ABC123 '. Isalnum () #判断是否存在符号
True
>> ' \t\r\n '. Isspace () #是否为空白字符
True
####### #去除字符串两端空白
>> a = ' \thello world '
>> A
' \thello World '
>> A.strip ()
' Hello World '
>> A.lstrip () #去除左边
>> A.rstrip () #去除右边
# # # #XXX. Strip () remove the specified characters at both ends
>> a = ' Hello world! '
>> a.strip ('! ')
' Hello World '
>> a.strip ('!he ')
' Llo World '
>> a.strip ('!dhe ')
' Llo worl '
>> A
' Hello world! '
>> A.strip (' W ') #无法去除中间字符
' Hello world! '
>> hi = ' Hello,world '
>> hi.split () #把字符串进行转换, convert to list,
[' Hello,world ']
>> hi.split (', ') #指定分割字符串的分隔符
[' Hello ', ' world ']
>> hi.replace (' O ', ' a ') #替换字符
' Hella,warld '
Python3 string manipulation