在 Python 中,字符串是一种非常常用的数据类型,提供了许多内置方法用于字符串操作。以下是字符串常用的函数(方法),并附带示例讲解。
1. 修改字符串内容
1.1 str.upper()
和 str.lower()
将字符串转换为大写或小写。
s = "Hello World"
print(s.upper()) # 输出: "HELLO WORLD"
print(s.lower()) # 输出: "hello world"
1.2 str.capitalize()
将字符串的首字母大写,其余字母小写。
s = "hello world"
print(s.capitalize()) # 输出: "Hello world"
1.3 str.title()
将字符串中每个单词的首字母大写。
s = "hello world"
print(s.title()) # 输出: "Hello World"
1.4 str.swapcase()
将字符串中的大写字母变为小写,小写字母变为大写。
s = "Hello World"
print(s.swapcase()) # 输出: "hELLO wORLD"
2. 字符串查找与替换
2.1 str.find()
和 str.rfind()
查找子字符串在字符串中的位置,返回索引。如果未找到,返回 -1
。
s = "hello world"
print(s.find("world")) # 输出: 6
print(s.rfind("o")) # 输出: 7
2.2 str.index()
和 str.rindex()
类似于 find()
,但如果子字符串不存在会抛出异常。
s = "hello world"
print(s.index("world")) # 输出: 6
2.3 str.replace()
用新字符串替换旧字符串。
s = "hello world"
print(s.replace("world", "Python")) # 输出: "hello Python"
3. 判断字符串内容
3.1 str.startswith()
和 str.endswith()
检查字符串是否以指定前缀或后缀开头/结尾。
s = "hello world"
print(s.startswith("hello")) # 输出: True
print(s.endswith("Python")) # 输出: False
3.2 str.isalpha()
检查字符串是否只包含字母(不含空格、数字等)。
s = "hello"
print(s.isalpha()) # 输出: True
3.3 str.isdigit()
检查字符串是否只包含数字。
s = "12345"
print(s.isdigit()) # 输出: True
3.4 str.isalnum()
检查字符串是否只包含字母和数字。
s = "abc123"
print(s.isalnum()) # 输出: True
3.5 str.isspace()
检查字符串是否只包含空白字符(如空格、制表符)。
s = " "
print(s.isspace()) # 输出: True
3.6 str.islower()
和 str.isupper()
检查字符串是否全为小写或大写。
s = "hello"
print(s.islower()) # 输出: True
print(s.isupper()) # 输出: False
4. 字符串分割与拼接
4.1 str.split()
将字符串按指定分隔符拆分为列表。
s = "hello world python"
print(s.split()) # 输出: ['hello', 'world', 'python']
4.2 str.splitlines()
按行分割字符串,返回一个列表。
s = "hello\nworld\npython"
print(s.splitlines()) # 输出: ['hello', 'world', 'python']
4.3 str.join()
将列表中的字符串按指定分隔符拼接。
words = ["hello", "world", "python"]
print(" ".join(words)) # 输出: "hello world python"
5. 去除空白字符
5.1 str.strip()
移除字符串两端的指定字符(默认为空格)。
s = " hello world "
print(s.strip()) # 输出: "hello world"
5.2 str.lstrip()
和 str.rstrip()
只移除左侧或右侧的指定字符。
s = " hello world "
print(s.lstrip()) # 输出: "hello world "
print(s.rstrip()) # 输出: " hello world"
6. 字符串对齐
6.1 str.center()
将字符串居中,并用指定字符填充两边。
s = "hello"
print(s.center(10, "-")) # 输出: "--hello---"
6.2 str.ljust()
和 str.rjust()
将字符串左对齐或右对齐,并用指定字符填充。
s = "hello"
print(s.ljust(10, "-")) # 输出: "hello-----"
print(s.rjust(10, "-")) # 输出: "-----hello"
7. 其他常用函数
7.1 str.count()
统计子字符串在字符串中出现的次数。
s = "hello world"
print(s.count("o")) # 输出: 2
7.2 str.zfill()
在字符串前填充零,直到达到指定宽度。
s = "42"
print(s.zfill(5)) # 输出: "00042"
7.3 str.partition()
和 str.rpartition()
按指定分隔符拆分字符串,返回三部分(分隔符前、分隔符、分隔符后)。
s = "hello world python"
print(s.partition("world")) # 输出: ('hello ', 'world', ' python')
7.4 str.encode()
和 str.decode()
用于字符串的编码和解码操作。
s = "hello"
encoded = s.encode("utf-8") # 编码
print(encoded) # 输出: b'hello'
print(encoded.decode("utf-8")) # 解码输出: "hello"
8. 示例:综合运用
s = " Hello World! Python is awesome. "
# 修改字符串
print(s.lower())
print(s.strip())
# 查找与替换
print(s.find("Python"))
print(s.replace("awesome", "amazing"))
# 分割与拼接
words = s.strip().split()
print(" | ".join(words))
# 判断内容
print(s.startswith(" "))
print(s.isalpha())
输出:
hello world! python is awesome.
Hello World! Python is awesome.
19
Hello World! Python is amazing.
Hello | World! | Python | is | awesome.
True
False
这些方法几乎覆盖了字符串操作的方方面面,帮助你在处理文本时更加高效!