Python-10 字符串-各种奇葩的内置方法

python字符串的方法和注释

1、capitalize()  

把字符串的第一个字符大写

>>> str = 'beijing'
>>> str.capitalize()
'Beijing'

2、casefold() 

把一个字符串的所有字符小写

>>> str = 'Beijing'
>>> str.casefold()
'beijing'
>>> 

3、center(width) 

将字符串居中,并使用空格填充至长度width的新字符串

>>> str.center(10)
' Beijing  '
>>> 

4、str.count(sub[,start[,end]])

 返回sub在字符串里边出现的次数,start和end参数表示范围,可选。

>>> str.count('i',1,9)
2
>>> str.count('i',1,3)
1
>>> str.count('i')
2
>>> 

5、encode(encoding='utf-8',errors='strict')

 以encoding指定的编码格式对字符串进行编码。


6、endswith(suffix[,start[,end]])

检查字符串是否以suffix子字符串结束,如果是返回True,否返回False。star和end参数表示范围,可选。

>>> len(str)
7
>>> str.endswith('ing')
True
>>> str.endswith('ing',2,6)
False
>>> str.endswith('ing',3,7)
True
>>> 

7、expandtabs(tabsize=8

把字符串中的tab符号(\t)转换为空格,如不指定参数,默认的空格数tabsize=8

>>> str='beijing\tshanghai\tshenzhen'
>>> str
'beijing\tshanghai\tshenzhen'
>>> str.expandtabs()
'beijing shanghai        shenzhen'
>>> str.expandtabs(4)
'beijing shanghai    shenzhen'
>>> 

8、find(sub[,start[,end]]

检测sub是否包含在字符串中,如果有则返回索引值,否则返回-1,start和end参数表示范围,可选。

>>> str = 'chengdu'
>>> str.find('c')
0
>>> str.find('s')
-1
>>> str.find('sd',1,7)
-1
>>> str.find('gd',0,7)
4
>>> 'gd' in str
True
>>> 'dg' in str
False
>>> 

9、index(sub[,start[,end]])  

跟find方法一样,不过如果sub不在字符串中会产生一个异常

>>> str.index('dg')
Traceback (most recent call last):
  File "<pyshell#45>", line 1, in <module>
    str.index('dg')
ValueError: substring not found
>>> str	.index('gd')
4
>>> str.index('gd',0,7)
4
>>> 

10、isalnum() 

如果一个字符串至少有一个字符并且所有字符都是字母或数字则返回True,否则返回False。

>>> 'ASWEaerC1'.isalnum()
True
>>> 'ASWE aerC1'.isalnum()
False
>>> 

11、isalpha() 

如果至少有一个字符并且所有字符都是字母则返回True,否则返回False。

>>> 'easdDF'.isalpha()
True
>>> 'erewfg12'.isalpha()
False
>>> 

12、isdecimal() 

如果字符串只包含十进制数字则返回True,否则返回False。

>>> '1234343545'.isdecimal()
True
>>> 'qwee121313'.isdecimal()
False
>>> 

13、isdigit() 

如果字符串只包含数字则返回True,否则返回False。

>>> '45678'.isdigit()
True
>>> '1.2345'.isdigit()
False
>>> 

14、islower() 

如果字符串中至少包含一个区分大小写的字符,并且这些字符都是小写,则返回True,否则返回False。

>>> b'hello world'.islower()
True
>>> b'Hello World'.islower()
False
>>> 

15、isnumeric(

如果字符串中只包含数字字符,则返回True,否则返回False。

>>> '1244'.isnumeric()
True
>>> '23556q'.isnumeric()
False
>>> 

16、isspace(

如果字符串中只包含空格,则返回True,否则返回False。

>>> ' '.isspace()
True
>>> '1as we'.isspace()
False
>>> 

17、istitle(

如果字符串是标题化(所有的单词都是以大写开始,其余字母均小写),则返回True,否则返回False。

>>> b'Hello World'.istitle()
True
>>> b'hello World'.istitle()
False
>>> 

18、isupper(

如果字符串至少包含一个区分大小写的字符,并且这些字符都是大写,则返回True,否则返回False。

>>> b'HELLO WORLD'.isupper()
True
>>> b'HEllo world'.isupper()
False
>>> 

19、join(sub) 

以字符串作为分隔符,插入到sub中所有字符之间。

>>> 'hello'.join('12345')
'1hello2hello3hello4hello5'
>>> 

20、ljust(width[,fillbyte])

返回一个左对齐的字符串,并使用空格填充至长度为width的新字符串

>>> 'hello'.ljust(10)
'hello     '
>>> 'hello'.center(10)
'  hello   '
>>> 'hello'.rjust(10)
'     hello'
>>> 

21、lower()

转换字符串中所有大写字符为小写

>>> 'HellO World'.lower()
'hello world'
>>> 

22、lstrip([chars])

去掉字符串左边的所有空格,chars参数可以定制删除的字符,可选。

>>> '  hello   '.lstrip()
'hello 
>>> b'www.google.com'.lstrip(b'comwz.')
b'google.com'
>>> 

23、partition(sep)

找到子字符串seq,把字符串分成3个元组(pre_seq,seq,fol_seq),如果子字符串中不包含seq则返回('元字符串','','')

>>> 'precipitate'.partition('ip')
('prec', 'ip', 'itate')
>>> 
>>> 'precipitate'.partition('ipp')
('precipitate', '', '')
>>> 

24、replace(old,new[, count])

把字符串中old子字符串替换成new子字符串,如果count指定,则替换不超过count次。

>>> 'hello world'.replace('hell','HELL')
'HELLo world'
>>> 
>>> 'hello hello hello'.replace('he','He',2)
'Hello Hello hello'
>>> 

25、rfind(sub[,start[, end]])

类似于find()方法,不过是从右边开始查找。

>>> 'hello,world'.rfind('llo',1,9)
2
>>> 

26、rindex(sub[,start[, end]])

类似于index()方法,不过是从右边开始。

>>> 'hello world'.rindex('ge')
Traceback (most recent call last):
  File "<pyshell#38>", line 1, in <module>
    'hello world'.rindex('ge')
ValueError: substring not found
>>> 'hello world'.rindex('ll')
2
>>> 'hello world'.rindex('ll',5,11)
Traceback (most recent call last):
  File "<pyshell#40>", line 1, in <module>
    'hello world'.rindex('ll',5,11)
ValueError: substring not found
>>> 'hello world'.rindex('ll',1,11)
2
>>> 

27、rjust(width[,fillbyte])

返回一个右对齐的字符串,并使用空格填充至长度为width的新字符串。

>>> 'hello'.ljust(10)
'hello     '
>>> 'hello'.center(10)
'  hello   '
>>> 'hello'.rjust(10)
'     hello'
>>> 

28、rpartition(sep)

类似于partition()方法,不过是从右边开始查找。

>>> 'hello world'.rpartition('ll')
('he', 'll', 'o world')
>>> 'hello world'.partition('ll')
('he', 'll', 'o world')
>>> 

29、rstrip([chars]

删除字符串末尾的空格。

>>> 'hello  '.rstrip()
'hello'
>>> 

30、split(sep=None,maxsplit=-1)

不带参数默认返回是以空格为分隔符切片字符串,如果maxsplit参数有设置,则仅分隔maxsplit个子字符串,返回切片的子字符串拼接的列表。

>>> 'hello world'.split()
['hello', 'world']
>>> 'hello,world'.split(',')
['hello', 'world']
>>> 'hello,world'.split()
['hello,world']
>>> 'hello,world,hello'.split(',')
['hello', 'world', 'hello']
>>> 'hello,world,hello'.split(',',maxsplit=1)
['hello', 'world,hello']
>>> 'hello,world,hello'.split(',',maxsplit=2)
['hello', 'world', 'hello']
>>> 'hello,world,hello'.split(',',maxsplit=3)
['hello', 'world', 'hello']
>>> 


31、splitlines(keepends=False)

按照'\n'分隔,返回一个包含各行作为元素的列表,如果keepends参数指定,则返回前keepends行。

>>> 'ab c\n\nde fg\rkl\r\n'.splitlines(keepends=True)
['ab c\n', '\n', 'de fg\r', 'kl\r\n']
>>> 'ab c\n\nde fg\rkl\r\n'.splitlines()
['ab c', '', 'de fg', 'kl']
>>> 

32、startswith(prefix[,start[, end]])

检查字符串是否以prefix开头,是返回True,否则返回False。start和end参数可以指定范围检查,可选。

>>> 'hello'.startswith('h')
True
>>> 'hello'.startswith('e',1,5)
True
>>> 'hello'.startswith('e',0,5)
False
>>> 

33、strip([chars])

删除字符串前边和后边的所有空格,chars参数可以定制删除的字符,可选。

>>> '   spacious   '.strip()
'spacious'
>>> b'www.example.com'.strip(b'cmowz.')
b'example'
>>> 'www.example.com'.strip('cmowz.')
'example'
>>> 

34、swapcase()

反转字符串中的大小写。

>>> 'Hello World'.swapcase()
'hELLO wORLD'
>>> 

35、title()

返回标题化(所有单词都是以大写开始,其余字母均小写)的字符串。

>>> 'Hello world'.title()
'Hello World'
>>> 'hello world'.title()
'Hello World'
>>> 

36、translate(table[,delete])

根据table的规则(可以由str.maketrans('a','b')定制)转换字符串中的字符。

>>> b'read this short text'.translate(None, b'aeiou')
b'rd ths shrt txt'
>>> str='aaaafffaaa'
>>> str.translate(str.maketrans('a','b'))
'bbbbfffbbb'
>>> str.maketrans('a','b')
{97: 98}
>>> str
'aaaafffaaa'
>>> str.translate({97: 98})
'bbbbfffbbb'
>>> 

37、upper()

转化字符串中所有小写字符为大写。

>>> 'hello'.upper()
'HELLO'
>>> 'hElLo'.upper()
'HELLO'
>>> 

38、zfill(width)

返回长度为width的字符串,原字符串右对齐,前面用0填充。

>>> '23'.zfill(5)
'00023'
>>> '-23'.zfill(5)
'-0023'
>>> 








  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值