s = " hello world "
print(s.strip()) # "hello world"
print(s.lstrip()) # "hello world "
print(s.rstrip()) # " hello world"
url = "https://www.example.com "
print(url.removeprefix("https://")) # "www.example.com"
filename = "report_final.pdf"
print(filename.removesuffix(".pdf")) # "report_final"
这两个方法在 3.9 版本引入,提供了更直观的前后缀删除功能。
s = "Python3"
print(s.upper()) # "PYTHON3"
print(s.lower()) # "python3"
print(s.capitalize()) # "Python3"
print(s.swapcase()) # "pYTHON3"
print("hello world".title()) # "Hello World"
s = "12345"
print(s.isdigit()) # True
print(s.isnumeric()) # True
print(s.isalpha()) # False
print(s.isalnum()) # True
print(s.islower()) # False
print(s.isupper()) # False
txt = "abracadabra"
print(txt.find("ra")) # 2
print(txt.rfind("ra")) # 9
print(txt.count("a")) # 5
s = "data_report.csv"
print(s.startswith("data_")) # True
print(s.endswith(".csv")) # True
line = "name:age:city"
print(line.split(":")) # ['name', 'age', 'city']
print(line.rsplit(":", 1)) # ['name:age', 'city']
print(line.partition(":")) # ('name', ':', 'age:city')
print(line.rpartition(":")) # ('name:age', ':', 'city')
words = ["Python", "is", "awesome"]
print(" ".join(words)) # "Python is awesome"
s = "I love Python. Python is great."
print(s.replace("Python", "Java")) # "I love Java. Java is great."
import re
s = "2025-11-20"
print(re.sub(r"-", "/", s)) # "2025/11/20"
s = "42"
print(s.zfill(5)) # "00042"
print(s.rjust(5, "*")) # "***42"
print(s.ljust(5, "-")) # "42---"
print(s.center(7, "~")) # "~42~~~"
str 与 bytes 之间转换。
# 读取文本文件,去除每行首尾空白,统计出现的单词数
from collections import Counter
with open('sample.txt', encoding='utf-8') as f:
words = []
for line in f:
line = line.strip() # 去除首尾空白
words.extend(line.lower().split()) # 小写并拆分
counter = Counter(words)
print(counter.most_common(5)) # 输出出现频率最高的 5 个单词
以上示例覆盖了常用的字符串操作方法,帮助在实际项目中快速完成文本处理、数据清洗等任务。