31个必备的Python字符串方法,建议收藏!("Python必备:31个实用字符串方法汇总,建议收藏!")
原创
一、字符串的创建与转换
字符串是Python中非常常用的数据类型,以下是一些创建和转换字符串的方法。
1. str() - 将对象演化为字符串
num = 123
str_num = str(num)
print(str_num) # 输出: '123'
2. chr() - 将整数演化为对应的ASCII字符
char = chr(97)
print(char) # 输出: 'a'
3. ord() - 将字符演化为对应的ASCII整数
num = ord('a')
print(num) # 输出: 97
二、字符串的检索与判断
以下是一些用于检索和判断字符串的方法。
4. find() - 查找子字符串的位置(返回第一个匹配项的索引)
str1 = "Hello, World!"
index = str1.find("World")
print(index) # 输出: 6
5. rfind() - 从右侧开端查找子字符串的位置
str1 = "Hello, World! World is beautiful."
index = str1.rfind("World")
print(index) # 输出: 12
6. index() - 查找子字符串的位置(如果不存在会抛出异常)
str1 = "Hello, World!"
index = str1.index("World")
print(index) # 输出: 6
7. rindex() - 从右侧开端查找子字符串的位置(如果不存在会抛出异常)
str1 = "Hello, World! World is beautiful."
index = str1.rindex("World")
print(index) # 输出: 12
8. count() - 计算子字符串出现的次数
str1 = "Hello, World! World is beautiful."
count = str1.count("World")
print(count) # 输出: 2
9. startswith() - 判断字符串是否以指定的子字符串开端
str1 = "Hello, World!"
result = str1.startswith("Hello")
print(result) # 输出: True
10. endswith() - 判断字符串是否以指定的子字符串终结
str1 = "Hello, World!"
result = str1.endswith("World")
print(result) # 输出: True
11. isalpha() - 判断字符串是否只包含字母
str1 = "Hello"
result = str1.isalpha()
print(result) # 输出: True
12. isdigit() - 判断字符串是否只包含数字
str1 = "123"
result = str1.isdigit()
print(result) # 输出: True
13. isalnum() - 判断字符串是否只包含字母和数字
str1 = "Hello123"
result = str1.isalnum()
print(result) # 输出: True
14. isspace() - 判断字符串是否只包含空白字符
str1 = " "
result = str1.isspace()
print(result) # 输出: True
15. istitle() - 判断字符串是否为标题格式(每个单词的首字母大写)
str1 = "Hello World"
result = str1.istitle()
print(result) # 输出: True
16. islower() - 判断字符串是否只包含小写字母
str1 = "hello"
result = str1.islower()
print(result) # 输出: True
17. isupper() - 判断字符串是否只包含大写字母
str1 = "HELLO"
result = str1.isupper()
print(result) # 输出: True
三、字符串的修改与操作
以下是一些用于修改和操作字符串的方法。
18. capitalize() - 将字符串的首字母大写
str1 = "hello world"
result = str1.capitalize()
print(result) # 输出: 'Hello world'
19. title() - 将字符串中每个单词的首字母大写
str1 = "hello world"
result = str1.title()
print(result) # 输出: 'Hello World'
20. lower() - 将字符串演化为小写
str1 = "HELLO WORLD"
result = str1.lower()
print(result) # 输出: 'hello world'
21. upper() - 将字符串演化为大写
str1 = "hello world"
result = str1.upper()
print(result) # 输出: 'HELLO WORLD'
22. swapcase() - 将字符串中的大小写字母互换
str1 = "Hello World"
result = str1.swapcase()
print(result) # 输出: 'hELLO wORLD'
23. replace() - 替换字符串中的子字符串
str1 = "Hello World"
result = str1.replace("World", "Python")
print(result) # 输出: 'Hello Python'
24. join() - 将字符串序列连接成一个新的字符串
str_list = ["Hello", "World", "Python"]
result = "-".join(str_list)
print(result) # 输出: 'Hello-World-Python'
25. split() - 将字符串分割成列表
str1 = "Hello World Python"
str_list = str1.split(" ")
print(str_list) # 输出: ['Hello', 'World', 'Python']
26. rsplit() - 从右侧开端分割字符串
str1 = "Hello World Python"
str_list = str1.rsplit(" ")
print(str_list) # 输出: ['Hello', 'World', 'Python']
27. partition() - 将字符串分割成三部分,并返回一个元组
str1 = "Hello World Python"
result = str1.partition("World")
print(result) # 输出: ('Hello ', 'World', ' Python')
28. rpartition() - 从右侧开端分割字符串,并返回一个元组
str1 = "Hello World Python"
result = str1.rpartition("World")
print(result) # 输出: ('Hello World ', 'World', '')
29. strip() - 移除字符串首尾的指定字符或空白字符
str1 = " Hello World "
result = str1.strip()
print(result) # 输出: 'Hello World'
30. lstrip() - 移除字符串左侧的指定字符或空白字符
str1 = " Hello World "
result = str1.lstrip()
print(result) # 输出: 'Hello World '
31. rstrip() - 移除字符串右侧的指定字符或空白字符
str1 = " Hello World "
result = str1.rstrip()
print(result) # 输出: ' Hello World'
总结
以上是Python中31个非常实用的字符串方法,掌握这些方法可以帮助我们更高效地处理字符串数据。在实际编程中,灵活运用这些方法可以让我们在处理字符串时游刃有余。建议收藏并经常性复习,以加深懂得。