text = "Hello\nWorld\nPython"
lines = text.splitlines()
print(lines) # 输出: ['Hello', 'World', …
text = "Hello\nWorld\nPython"
lines = text.splitlines()
print(lines) # 输出: ['Hello', 'World', 'Python']
6. 使用字符串方法 center()
、ljust()
和 rjust()
对齐字符串
这些方法可以让字符串在指定的宽度内居中、左对齐或右对齐。
s = "hello"
print(s.center(10, '*')) # 输出: **hello***
print(s.ljust(10, '-')) # 输出: hello-----
print(s.rjust(10, '=')) # 输出: =====hello
7. 使用字符串方法 capitalize()
和 title()
将字符串首字母大写或每个单词的首字母大写
这两个方法可以帮助我们规范化字符串的格式。
s = "hello world"
print(s.capitalize()) # 输出: Hello world
print(s.title()) # 输出: Hello World
8. 使用字符串方法 lower()
和 upper()
将字符串转换为小写或大写
这两个方法可以方便地将字符串转换为小写或大写形式。
s = "Hello, World!"
print(s.lower()) # 输出: hello, world!
print(s.upper()) # 输出: HELLO, WORLD!
9. 使用字符串方法 isalpha()
、isdigit()
和 isalnum()
判断字符串的类型
这些方法可以帮助我们判断字符串是否只包含字母、数字或字母和数字的组合。
s1 = "hello"
s2 = "123"
s3 = "hello123"
s4 = "hello 123"
print(s1.isalpha()) # 输出: True
print(s2.isdigit()) # 输出: True
print(s3.isalnum()) # 输出: True
print(s4.isalnum()) # 输出: False
10. 使用字符串方法 startswith()
和 endswith()
判断字符串是否以指定的前缀或后缀开始或结束
这两个方法可以帮助我们快速判断字符串是否以某个前缀或后缀开始或结束。
filename = "example.txt"
print(filename.startswith("ex")) # 输出: True
print(filename.endswith(".txt")) # 输出: True
11. 使用正则表达式进行复杂的字符串匹配和替换
当处理复杂的字符串匹配和替换时,可以使用 Python 的 re
模块来操作正则表达式。
import re
s = "hello 123 world 456"
numbers = re.findall(r'\d+', s)
print(numbers) # 输出: ['123', '456']
new_s = re.sub(r'\d+', '###', s)
print(new_s) # 输出: hello ### world ###
12. 使用字符串方法 count()
统计子串出现的次数
count()
方法可以统计子串在字符串中出现的次数。
s = "hello hello world"
print(s.count("hello")) # 输出: 2
13. 使用字符串方法 find()
或 index()
查找子串的位置
find()
方法可以返回子串在字符串中第一次出现的位置,如果未找到则返回 -1
;而 index()
方法也是查找子串的位置,但是如果未找到会抛出异常。
s = "Hello, World!"
print(s.find("World")) # 输出: 7
print(s.find("Python")) # 输出: -1
print(s.index("World")) # 输出: 7
# print(s.index("Python")) # 会抛出 ValueError 异常