python字符串操作
字符串操作记录
python3
a="\tit's time for the big show." b='31abc' print(a.capitalize()) #首字母大写 print(a.count('t')) #统计指定字符个数(t) print(a.center(50,'#')) #打印50个字符如果不够就用#补上 print(a.encode()) #字符串转换成二进制 print(a.endswith('w.')) #判断是不是以w.结尾 print(a.expandtabs(20)) #把tab键转换为20个字符 print(a[a.find('time'):]) #字符换time的位置,字符串可转化为列表,本例是去除time以后的字符串 a="{name} it's time for the big show." print(a.format(name='Kobe')) #格式化 print(a.format_map( {'name':'Jodan'})) #字典格式化 print(b.isalnum()) #字符换测试,判断字符串是不是只包含英文字符和数字 print('Abc'.isalpha()) #字符换测试,判断字符串是不是纯英文字符 print('123910'.isdecimal()) #字符换测试,判断字符串是不是十进制数字 print('123910.0'.isdigit()) #字符换测试,判断字符串是不是数字 print('_12A'.isidentifier()) #字符换测试,判断字符串是不是合法的标识符(即变量名) print('a_12A'.islower()) #字符换测试,判断字符串是不是小写 print('199'.isnumeric()) #字符换测试,判断字符串是不是数值 和 isdigit功能相同 print('Allen big'.istitle()) #字符换测试,判断字符串是不是taitle,全部首字母大写 print('Allen Big'.isprintable()) #字符换测试,判断字符串是不是可打印文件(tty drive设备都不是) print('ALLEN IVER'.isupper()) #字符换测试,判断字符串是不是全部大写 print('ALLEN IVER'.join('****')) #分割字符串 print('###'.join(['1','adf','allen'])) #将列表中的值用###拼接成字符串 print('Hello world'.lower())#变小写 print('Hello world'.upper())#变大写 print('\nHello world\t'.lstrip())#去掉左边的换行符或者空格 print('\nHello world\t'.rstrip())#去掉右边的换行符或者空格 print('\nHello world\t'.strip())#左右的空格和换行符 print('--------------') print('allen'.replace('l','L',1))#字符替换,默认全部替换本次替换1个,从左边数 print('allen'.rfind('l'))#找到最右边值的下标 print('allen #en'.split('#'))#转换为列表,默认是空格为分隔符 print('allen \n#en'.splitlines())#转换为列表,按照换行符 print('allen #en'.swapcase())#大小写转换 print('allen #en'.title())#全部字符首字母大写 print('Alle_n #en'.upper())#全变大写 print('Alle_n #en'.zfill(20))#用0凑足20个字符
# ljust(len,str)字符向左对齐,用str补齐长度
# rjust(len,str)字符向右对齐,用str补齐长度
# rjust(len,str)字符中间对齐,用str补齐长度