Python内置字符串函数的用法

print('lmy name is lizhenlei'.capitalize())             #首字母大写
print('Lmy Name is LIZHENLEI123#'.casefold()) #全变成小写
print('Lmy Name is LIZHENLEI123#'.center(50, '-')) #一共有50个字符,把字符串放到中间
print('Lmy Name is LIZHENLEI123#'.count('L')) #查询一共有多少个L
print('你好'.encode('utf-8')) #转换编码方式
print('Lmy Name is LIZHENLEI123#'.endswith('#')) #判断结尾是不是指定字符
print('Lmy Name is \tLIZHENLEI123#'.expandtabs(50)) #设定字符串中一个Tab的长度
print('me{} is{} 3#{}'.format('aaaa', 'bbbb', 'cccc')) #格式化字符串
print('Lmy Name is LIZHENLEI123#'.find('m')) #从左向右找到想要查找的内容,并且返回查找的第一个字母的下标
people = {'name':'lizhenlei', 'age':23}
print('My name is {name}, I {age}'.format_map(people)) #格式化字符串输出
print('Lmy Name is LIZHENLEI123#'.isdigit())
print('123'.isdigit()) #是否为数字
print('123abcBAC'.isalnum()) #是否为字母和数字
print('123abcBAC'.isalpha()) #不知道啥意思
print('123abcBAC'.isdecimal()) #不知啥意思
print('123abcBAC'.isidentifier()) #判断字符串是不是一个合法的变量名
print('123abc'.islower()) #判断字符串中字母是不是都是小写
print('123abc'.isnumeric()) #不知道啥意思
print('dobi123\n'.isprintable()) #判断字符串是否为可打印的,或者字符串为空
print(' '.isspace()) #判断字符串是否为空格
print('My Name Is Lizhenlei'.istitle()) #判断字符串中的字符是否是首字母大写,其会忽视非字母字符。
print('ABC'.isupper()) #判断字符串中是否都为大写字母
print('ABC'.index('A')) #与 find() rfind() 类似,不同的是如果找不到,就会引发 ValueError。
print('-'.join(['2012', '3', '12'])) #用指定的字符串,连接元素为字符串的可迭代对象。
print('ABC'.ljust(10,'a')) #返回指定长度的字符串,字符串内容居左(右)如果长度小于字符串
# 长度,则返回原始字符串,默认填充为 ASCII 空格,可指定填充的字符串。
print('ABC123#'.lower()) #将字符串中的大写字母变成小写字母
print('\n ABC123# \n'.lstrip()) #去掉左边的空格和回车
a = 'dobi is a dog'
table = str.maketrans('dobi', 'alph')
print(a.translate(table)) #maktrans 是一个静态方法,用于生成一个对照表,以供 translate 使用。
#如果 maktrans 仅一个参数,则该参数必须是一个字典,字典的 key 要么是一个 Unicode 编码(一个整数),
# 要么是一个长度为 1 的字符串,字典的 value 则可以是任意字符串、None或者 Unicode 编码。
print('ABC123#'.partition('A')) #按字符串的内容切割字符串
print('dog wow wow jiao'.replace('wow', 'wang')) #按规则替换字符串的内容
print('ABC123#A'.rfind("A")) #从右向左找字母
print('ABC123#A'.rindex('A')) #从右向左找字母 如果找不到,就会引发 ValueError。
print('ABC123#A'.rjust(50, 'a')) #在字符串左边补空格
print('ABC123#A'.rpartition('A')) #按规则切割字符串
print('ABC123#A'.rsplit()) #不太清楚
print('ABC123#A'.rstrip()) #不太清楚
print('ABC123#A'.split())
print('ABC123#A'.splitlines())
print('ABC123#A'.startswith())
print('ABC123#A'.strip())
print('ABC123#A'.swapcase())
print('ABC123#A'.translate())
print('ABC123#'.title()) #将字符串中每个“单词”首字母大写。其判断“单词”的依据则是
# 基于空格和标点,所以应对英文撇好所有格或一些英文大写的简写时,会出错。
print('abc123#'.upper()) #将字符串中的小写字母变成大写字母
print('abc123#'.zfill(10)) #用 '0' 填充字符串,并返回指定宽度的字符串。
posted @ 2018-04-02 20:36  阳光下的老男孩  阅读(150)  评论(0编辑  收藏  举报