1. 切片
通过切片,我们可以访问子字符串。
>>> s = ' hello '
>>> s[3:8]
'hello'
2. strip()
用于移除字符串头尾指定的字符(默认为空格或换行符)
>>> s = ' hello '
>>> s.strip()
'hello'
>>> s = '###hello###'
>>> s.strip('#')
'hello'
>>> s = '\n\n \t hello\n'
>>> s.strip('\n')
' \t hello'
>>> s = ' \n \t hello\n'
>>> s.strip('\n')
' \n \t hello'
# 不是以\n开头,所以左边\n没有被移除
3.4. lstrip() 和 rstrip()
lstrip([chars]):用于截掉字符串左边的空格或指定字符。
rtrip([chars]):用于截掉字符串右边的空格或指定字符。
>>> s = ' hello '
>>> s.lstrip()
'hello '
>>> s.rstrip()
' hello'
>>> s.lstrip('#')
'hello###'
>>> s.rstrip('#')
'###hello'
>>> s = 'www.example.com'
>>> s.strip('.wcmo')
'example'
# 如果参数是一个字符串,则这些所有出现的字符都将被删除,而不是给定的字符串。
5.6. removeprefix() 和 removesuffix()
如前所述,strip、lstrip 和 rstrip 会删除所有传递的参数字符串。因此,如果我们只想删除给定的字符串,我们可以使用 removeprefix 和 removesuffix。
>>> s = 'Arthur: three!'
>>> s.lstrip('Arthur: ')
'ee!'
>>> s = 'Arthur: three!'
>>> s.removeprefix('Arthur: ')
'three!'
>>> s = 'Arthur: three!'
>>> s.removesuffix('three!')
'Arthur: '
7. replace()
把字符串中的 old(旧字符串) 替换成 new(新字符串),如果指定第三个参数max,则替换不超过 max 次。
>>> s = ' \n \t hello\n'
>>> s.replace('\n', '', 1)
' \t hello\n'
8. re.sub()
如果我们想用一个特定的模式替换字符,可以使用正则表达式。
>>> import re
>>> s = "string methods in python"
>>> s2 = re.sub("\s+" , "-", s)
>>> s2
'string-methods-in-python'
有关正则表达式的更多信息,请参考历史文章。
9. split()
通过指定分隔符对字符串进行切片,该方法将字符串分割成子字符串并返回一个由这些子字符串组成的列表。如果第二个参数 num 有指定值,则分割为 num+1 个子字符串。
>>> s = 'string methods in python'
>>> s.split()
['string', 'methods', 'in', 'python']
>>> s = 'string methods in python'
>>> s.split(' ', 2)
['string', 'methods', 'in python']
10. rsplit()
与 split() 功能相似,如果第二个参数 num 有指定值,从最右边开始拆分。
>>> s = 'string methods in python'
>>> s.rsplit()
['string', 'methods', 'in', 'python']
>>> s = 'string methods in python'
>>> s.rsplit(' ', 2)
['string methods', 'in', 'python']
11. join()
用于将序列中的元素以指定的字符连接生成一个新的字符串。
>>> lst = ['string', 'methods', 'in', 'python']
>>> s = ' '.join(lst)
>>> s
'string methods in python'
12.13.14. upper()、lower()、capitalize()
返回字符串的副本,其中功能分别是所有大小写字符都转换为大写、小写或第一个字符大写,其余字符小写。
>>> s = 'string methods in python'
>>> s.upper()
'STRING METHODS IN PYTHON'
>>> s.lower()
'string methods in python'
>>> s.capitalize()
'String methods in python'
15.16. islower()、isupper()
检查字符串是否仅由大字符或小字符组成。
>>> s = 'String methods in python'
>>> s.islower()
False
>>> s.isupper()
False
>>> s = 'string methods in python'
>>> s.islower()
True
>>> s = 'STRING METHODS IN PYTHON'
>>> s.isupper()
True
17.18.19. isalpha()、isnumeric()、isalnum()
isalpha():如果字符串中的所有字符只由字母或文字组成,则返回 True,否则返回 False。
isnumeric():如果字符串中的所有字符都是只由数字组成,则返回 True,否则返回 False。
isalnum():如果字符串中的所有字符是由字母和数字组成,则返回 True,否则返回 False。
>>> s = 'python'
>>> s.isalpha()
True
>>> s.isnumeric()
False
>>> s.isalnum()
True
>>> s = 'python666'
>>> s.isalpha()
False
>>> s.isnumeric()
False
>>> s.isalnum()
True
>>> s = 'python-666'
>>> s.isalpha()
False
>>> s.isnumeric()
False
>>> s.isalnum()
False
20. count()
用于统计字符串里某个字符出现的次数。可选参数为在字符串搜索的开始与结束位置。
>>> s = 'hello world'
>>> s.count('o')
2
>>> s.count('o', 0, 5)
1
21. find()
检测字符串中是否包含子字符串 str ,如果包含,返回的是索引值在字符串中的起始位置。如果不包含,返回-1。
>>> s = 'hello world'
>>> s.find('a')
-1
>>> s.find('o')
4
22. rfind()
返回字符串最后一次出现的位置,如果没有匹配项则返回-1。
>>> s = 'hello world'
>>> s.rfind('o')
7
23.24. startswith()、 endswith()
用于检查字符串是否是以指定子字符串开头/结尾,如果是则返回 True,否则返回 False。
>>> s = 'hello world'
>>> s.startswith('he')
True
>>> s.endswith('d')
True
25. partition()
用来根据指定的分隔符将字符串进行分割。如果未找到分隔符,则返回一个包含字符串本身的元组,后跟两个空字符串。
>>> s = 'Python is awesome!'
>>> s.partition('is')
('Python ', 'is', ' awesome!')
>>> s.partition('iss')
('Python is awesome!', '', '')
26.27.28 center()、ljust()、rjust()
center():返回一个指定的宽度 width 居中的字符串,可选参数为填充的字符,默认为空格。
ljust():返回一个指定的宽度 width ,字符串左对齐,可选参数为填充的字符,默认为空格。
rjust():返回一个指定的宽度 width ,字符串右对齐,可选参数为填充的字符,默认为空格。
>>> s = 'Python'
>>> s.center(10, '-')
'--Python--'
>>> s.ljust(10, '-')
'Python----'
>>> s.rjust(10, '-')
'----Python'
29. f-Strings
f-strings 可用于格式化字符串。更具可读性,更简洁,也更快!
>>> num = 1
>>> language = 'Python'
>>> s = f'{language} is the number {num} in programming!'
>>> s
'Python is the number 1 in programming!'
30. swapcase()
用于对字符串的大小写字母进行转换,即将大写字母转换为小写字母,小写字母会转换为大写字母。
>>> s = 'HELLO world'
>>> s.swapcase()
'hello WORLD'
31. zfill()
返回指定长度的字符串,原字符串右对齐,前面填充0。如果字符串有前缀 ('+'/'-') 则在前缀字符之后而不是之前插入填充。
>>> s = '168'
>>> s.zfill(6)
'000168'
>>> s = '-168'
>>> s.zfill(6)
'-00168'
?
文章创作不易,如果您喜欢这篇文章,请关注、点赞并分享给朋友。如有意见和建议,请在评论中反馈!
?