name='my name is dream'
print(name.capitalize())#首字母大写
print(name.count(‘’a‘’))#判断字符串里有多少个a
print(name.center(50,"-")#打印50个字符,不够用-凑
print(name.encode())#字符串改二进制
print(name.endswith('m'))#判断字符串是否以指定字符收尾
print(name.expandtabs(tabsize=30))#把tab键转成30个字符
print(name.find('i'))#找出第一个字母(i)的位置
names='my name is {name} and I am {year} years old'
print(names.format(name="dream",year=28))#format用于替换括号内字符
print(names.format_map({'name':‘dream’,‘year’:26}#同上
print('ad133'.isalnum())#判断是否为字符串或数字
print('ad133'.isalpha())#判断是否为纯英文字符
print('1A'.isdecimal())#判断字符是否为十进制否
print('1A'.isidentifier())#判断字符是否为合法变量
print('1A'.isspace())#判断是否为空格
print('Name'.istitle())#判断是否首字母大写,数字也可以
print('1A'.isprintable())#判断是否可打印,不常用
print('My name'.isupper())#判断是否为全大写
print('+'.join(['1','2','3','4']))#将字符串用符号连接
print(name.ljust(30,'-'))#打印30个字符在左边,不够在右边用’-’凑
print(name.rjust(30,'-'))#打印30个字符在右边,不够在左边用’-’凑
print('NAME'.lower())#把大写变小写
print('name'.lower())#把小写变大写
print('\nNAME'.lstrip())# 把左边去空格或字符
print('\nNAME\n'.rstrip())#把右边去空格或字符
print('\nNAME\n'.strip())#把两边去空格或字符
p=str.maketrans('abcdef','123456')#用于创建字符映射的转换表,两个字符串的长度必须相等一一对应
print('dream'.translate(p))#根据参数table给出的表(包含256个字符)转换字符串的字符
print('dreamd'.replace('d','D',1))#替换第一个字母d改大写
print('dreamd'.rfind('d'))#最右边值得下标是‘5’
print('dre am ad'.split())#把字符串按空格分成列表
print('dra e am ad'.split('a'))#把字符串按'a'分成列表,会去掉a
print('dra e\n am ad'.splitlines())#把字符串按换行分成列表,
print('dream ad'.swapcase())##把字符串都转换成大写
print('dream ad'.title())#首字符大写.
print('dream ad'.zfill(50))#50个字符用0填充
print('---')
转载于:https://www.cnblogs.com/buildydream/p/10005385.html