hw='Hello World\tk...'hw2='Hello World\t{k}...{look}'#首字母大写print(hw.capitalize()) #Hello world k...#统计字符串内的数量print(hw.count('o')) #2#打印50个字符,不够两边用-填充print(hw.center(50,'-')) #-----------------Hello World k...-----------------#转换为二进制print(hw.encode()) #b'Hello World\tk...'#判断字符串以什么结尾print(hw.endswith('.')) #是True否False#把\t转换为多少个空格print(hw.expandtabs(tabsize=20)) #Hello World k...#查找字符串的位置print(hw.find('World')) #6print(hw[6:]) #World k...#替换和格式换print(hw2.format(k='ken',look='pppp')) #Hello World ken...pppp#字典里的用法print(hw2.format_map({'k':'ken','look':'pppp'})) #Hello World ken...pppp#查找字符的位置print(hw.index('o')) #4#判定字符串内是否为字符或数字print('ASrdw...10993'.isalnum()) #Falseprint('ASrdw10993'.isalnum()) #True#判定字符串内是否为英文字符print('Adfooi'.isalpha()) #Trueprint('Adfooi002'.isalpha()) #False#判定字符串是否为十进制print('55121'.isdecimal()) #Trueprint('5A'.isdecimal()) #False#判定字符串是否为整数print('1558'.isdigit()) #经常用到 #Trueprint('1.1558'.isdigit()) #False#判定字符串是否为合法的标识符print('auia'.isidentifier()) #Trueprint('2auia'.isidentifier()) #False#判定字符串是否为小写print('aadc'.islower()) #Trueprint('aAdc'.islower()) # False#判定字符串是否为数字print('3A'.isnumeric()) #str类型的数字 #Faleprint('300234'.isnumeric()) #True#判定字符串是否为空格print('3A'.isspace()) #Falseprint(' '.isspace()) #True#判定字符串每个首字母是否为大写print('Hello World'.istitle()) #Trueprint('Hello world'.istitle()) #False#判定字符串能否打印,一般tty file,drive file无法打印print('Hello world'.isprintable())#判定字符串是否为大写print('Hello world'.isupper()) #Falseprint('HELLO WORLD'.isupper())#把列表转出为字符串,并且可以自定义隔断符print('=='.join(['a','b','c'])) #经常用到 #a==b==cprint(''.join(['a','b','c'])) #abc#右补齐print('HELLO WORLD'.ljust(50,'~')) #HELLO WORLD~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#左补齐print('HELLO WORLD'.rjust(50,'~')) #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~HELLO WORLD#把大写变成小写print('HELLO WORLD'.lower()) #hello world#把小写变成大写print('hello world'.upper()) #HELLO WORLD#默认用来去除开头字符、空白符(包括\n、\r、\t、' ',即:换行、回车、制表符、空格)print('\thello world'.lstrip()) #经常用到 #hello worldprint('hello world'.lstrip('h')) #ello world#默认用来去除结尾字符、空白符(包括\n、\r、\t、' ',即:换行、回车、制表符、空格)print('hello world\t'.rstrip()) #经常用到 #hello world#用来去除头尾字符、空白符(包括\n、\r、\t、' ',即:换行、回车、制表符、空格)print('\tthllo world\t'.strip()) #经常用到 #hello world#转换字符,把设定好的字符转换list = str.maketrans('abcdef','123456')print('hello world'.translate(list)) #h5llo worl4#替换字符串内容print('hello world'.replace('l','L')) #经常用到 #heLLo worLdprint('hello world'.replace('l','L',1)) #heLlo world#查找最右面的值print('hello world'.rfind('l')) #9#把字符串转为列表形式,默认空格来区分print('hello world'.split()) #['hello', 'world']print('1+2+3+4+5'.split('+')) #['1', '2', '3', '4', '5']#把字符串转为列表形式,默认回车来区分print('1\n2\n3\n4\n5'.splitlines()) #['1', '2', '3', '4', '5']#大写变小写,小写变大写print('Hello World'.swapcase()) #hELLO wORLD#每个首字符变大写print('hello world'.title()) #Hello World#print('hello world'.zfill(50)) #000000000000000000000000000000000000000hello world
来源:https://www.cnblogs.com/wxy1987/p/11007379.html