元组方法

Tup.count():计算元组中指定元素出现的次数


Tup.count('c')



Tup.index():在元组中从左到右查找指定元素,找到第一个就返回该元素的索引值



Tup.index('c')



字符串方法

s.capitalize():将字符串的首字符进行大写转换,如果首字符不是字母则不发生改变,如果是就将小写字母转换成大写字母



s = 'hello world'
s.capitalize()
'Hello world'



s.caseflod():返回一个将原字符串中的所有大写字母转换成小写字母



s = 'HELLO WORLD'
s.casefold()
'hello world'



s.center():收到一个参数,将字符串为指定宽度,将原字符串置于宽度的二分之一处;接收两个参数时,扩充同样的宽度,并将扩充的地方换为指定字符



s = 'hello'
s.center(20, '*')
'*******hello********'



s.encode():接受一个编码参数,并根据该编码将数据编码为Byte类型的数据



s = 'hello'
s.encode(encoding='utf-16')
b'\xff\xfeh\x00e\x00l\x00l\x00o\x00'



s.endswith():比较字符串结尾部分字符与指定字符是否相等,是返回True,否返回False



s = 'hello'
s.endswith('llo')
True
s.endswith('lle')
False



s.startswith():比较字符串开头部分字符与指定字符是否相等,是返回True,否返回False



s = 'hello'
s.endswith('llo')
True
s.endswith('lle')
False



s.find():在字符串中查找指定的对象,从左到右,一旦找到就返回对应位置的索引



s = 'hello'
s.find('l')
2



s.isdigit():如果字符串中全是数字字符,就返回True,否则返回False



s = 'hello'
s.isdigit()
False



s.isalpha():如果字符串中全是字母字符,就返回True,否则返回False



s = 'hello'
s.isalpha()
True



s.islower():如果字符串全是小写字符,就返回True,否则返回False



s = 'hello'
s.islower()
True



s,isupper():如果字符串全是大写字符,就返回True,否则返回False



s = 'hello'
s.isupper()
False



s.lower():将字符串中字符全部转化为小写字符



s = 'HELLO'
s.lower()
'hello'



s.upper():将字符串中的字符全部转化为大写字符



s = 'hello'
s.upper()
'HELLO'



s.expandtabs():将字符串中的\t转化为空格,默认tabsize=8



s = 'hello\t'
s.expandtabs()
'hello   '



s.isalnum():如果字符串中的字符全是数字或字母,返回True,否则返回False



s = 'hello'
s.isalnum()
True



s.isdecimal():检查字符串中是否只包含十进制字符,并且只应用于Unicode对象



s = 'hello'
s.isdecimal()
False



s.isidentifier():实际上就是把字符串的内容当做变量名来判断,判断是否字符串中的内容是否符合变量命名规则。



s = 'hello'
s.isidentifier()
True



s.isnumeric():检测字符串中是否只由数字组成,并且该方法是只针对Unicode对象



s = 'hello'
s.isnumeric()
False



s.isprintable():判断字符串中包含的字符是否全部都是可打印,字符串包含不可打印的字符,如转义字符,就返回False



s = 'hello'
s.isprintable()
True



s.istitle():检测字符串中的单词首字母拼写首字母是否为大写,其他字母为小写,



s = 'hello'
s.istitle()
False



s.isspace():如果字符串的内容只有空格,就返回True,否则返回False



s = 'hello'
s.isspace()
False



s.title():返回字符串中的单词首字母为大写



s = 'hello'
s.title()
'Hello'



s.ljust() s.rjust():返回原字符的左对齐,空格填充



s = 'hello'
s.ljust(10)
'hello     '



s.strip() s.lstrip() s.rstrip()



s = '   hello   '
s.strip()
'hello'



s.maketrans():生成一个字典,key为a字符串中的每个元素的ASCII吗的对应的值,value同理,两参数长度需相等



s = 'hello'
s.maketrans('a', 'h')
{97: 104}



s.partition() s.rpartition():根据指定的分隔符,返回分割后的元素,从左到右只分割一次



s = 'hello world'
s.partition('l')
('he', 'l', 'lo world')
s.rpartition('l')
('hello wor', 'l', 'd')



s.replace():接收两个参数,第一个参数表示需要被替换的元素,第二个参数表示替换的元素



s = 'hello'
s.replace('h', 'q')
'qello'



s.split() s.rsplit():接收或者不接受参数,用来分割字符串,返回列表



s = 'hello'
s.split('l')
['he', '', 'o']



s.splitlines():用来去除字符串中的\r \n,并返回一个列表



s = 'hello\n \t'
s.splitlines()
['hello', ' \t']



s.swapacase(): 大小写转换



s = 'hello WORLD'
s.swapcase()
'HELLO world'



s.zfill():返回指定长度的字符串,原字符串右对齐,前面填充0



s = 'hello'
s.zfill(10)
'00000hello'

s.join():将序列中的元素以指定的字符连接成一个新的字符串



s = ' '
s.join('world')
'w o r l d'