Python字符串处理:15个常用函数详解及示例229


Python 提供了丰富的字符串操作函数,使得文本处理变得高效便捷。熟练掌握这些函数对于任何 Python 开发者来说都至关重要。本文将详细介绍 15 个常用的 Python 字符串函数,并结合实际示例,帮助你深入理解其用法。

1. `len()`:获取字符串长度

len() 函数返回字符串中字符的个数。例如:
string = "Hello, world!"
length = len(string)
print(length) # 输出 13

2. `upper()` 和 `lower()`:转换字符串大小写

upper() 将字符串转换为大写,lower() 将字符串转换为小写。例如:
string = "Hello, World!"
uppercase = ()
lowercase = ()
print(uppercase) # 输出 HELLO, WORLD!
print(lowercase) # 输出 hello, world!

3. `strip()`、`lstrip()` 和 `rstrip()`:去除字符串两端或单端的空格或指定字符

strip() 去除字符串两端的空格或指定字符,lstrip() 去除左端,rstrip() 去除右端。例如:
string = " Hello, world! "
stripped = ()
lstripped = ()
rstripped = ()
print(stripped) # 输出 Hello, world!
print(lstripped) # 输出 Hello, world!
print(rstripped) # 输出 Hello, world!
string2 = "*Hello*"
stripped2 = ("*")
print(stripped2) # 输出 Hello

4. `split()`:分割字符串

split() 函数根据指定分隔符将字符串分割成列表。如果不指定分隔符,则默认使用空格。例如:
string = "apple,banana,orange"
fruits = (",")
print(fruits) # 输出 ['apple', 'banana', 'orange']
string2 = "This is a sentence"
words = ()
print(words) # 输出 ['This', 'is', 'a', 'sentence']

5. `join()`:连接字符串

join() 函数将列表中的字符串元素连接成一个字符串。例如:
fruits = ['apple', 'banana', 'orange']
joined_string = ", ".join(fruits)
print(joined_string) # 输出 apple, banana, orange

6. `replace()`:替换字符串

replace() 函数将字符串中的指定子串替换为另一个子串。例如:
string = "Hello, world!"
new_string = ("world", "Python")
print(new_string) # 输出 Hello, Python!

7. `find()` 和 `rfind()`:查找子串

find() 从左向右查找子串,rfind() 从右向左查找子串。如果找到,返回子串的起始索引;否则返回 -1。例如:
string = "Hello, world, world!"
index = ("world")
rindex = ("world")
print(index) # 输出 7
print(rindex) # 输出 17

8. `startswith()` 和 `endswith()`:检查字符串开头和结尾

startswith() 检查字符串是否以指定子串开头,endswith() 检查字符串是否以指定子串结尾。例如:
string = "Hello, world!"
starts_with_hello = ("Hello")
ends_with_exclamation = ("!")
print(starts_with_hello) # 输出 True
print(ends_with_exclamation) # 输出 True

9. `isdigit()`、`isalpha()`、`isalnum()`:检查字符串类型

isdigit() 检查字符串是否只包含数字,isalpha() 检查字符串是否只包含字母,isalnum() 检查字符串是否只包含字母或数字。例如:
string1 = "123"
string2 = "abc"
string3 = "abc123"
print(()) # 输出 True
print(()) # 输出 True
print(()) # 输出 True

10. `count()`:统计子串出现的次数

count() 函数统计指定子串在字符串中出现的次数。例如:
string = "Hello, world, world!"
count = ("world")
print(count) # 输出 2

11. `center()`、`ljust()`、`rjust()`:对齐字符串

center() 将字符串居中对齐,ljust() 左对齐,rjust() 右对齐。需要指定总宽度。例如:
string = "Hello"
centered = (15, "*")
left_justified = (15, "*")
right_justified = (15, "*")
print(centered) # 输出 Hello
print(left_justified) # 输出 Hello
print(right_justified) # 输出 Hello

12. `zfill()`:用0填充字符串左侧

zfill() 函数用 0 填充字符串左侧,直到达到指定的宽度。例如:
string = "123"
filled = (5)
print(filled) # 输出 00123

13. `partition()`:将字符串分成三部分

partition() 函数根据分隔符将字符串分成三部分:分隔符之前的部分,分隔符本身,分隔符之后的部分。如果找不到分隔符,则返回原字符串和两个空字符串。例如:
string = "apple,banana,orange"
parts = (",")
print(parts) # 输出 ('apple', ',', 'banana,orange')

14. `splitlines()`:将字符串按行分割

splitlines() 函数根据换行符将字符串分割成一个列表。例如:
string = "This is line 1.This is line 2.This is line 3."
lines = ()
print(lines) # 输出 ['This is line 1.', 'This is line 2.', 'This is line 3.']

15. `format()`:格式化字符串

format() 函数提供了一种灵活的字符串格式化方式,可以方便地插入变量值。例如:
name = "Alice"
age = 30
message = "My name is {} and I am {} years old.".format(name, age)
print(message) # 输出 My name is Alice and I am 30 years old.

以上就是 15 个常用的 Python 字符串函数,熟练运用这些函数可以大大提高你的 Python 代码效率和可读性。 记住在实际应用中,根据具体需求选择合适的函数,并结合其他 Python 特性,编写出更优雅、更高效的代码。

2025-05-09


上一篇:Python 函数交换:深入探讨多种实现方法及性能分析

下一篇:Python字符串元素比较:详解方法、技巧及应用场景