Python 字符串(str)常用操作与示例

1. 去除空白与前后缀

strip / lstrip / rstrip

s = "   hello world   "
print(s.strip())   # "hello world"
print(s.lstrip()) # "hello world   "
print(s.rstrip()) # "   hello world"
    

removeprefix / removesuffix(Python 3.9+)

url = "https://www.example.com "
print(url.removeprefix("https://"))   # "www.example.com"
filename = "report_final.pdf"
print(filename.removesuffix(".pdf"))  # "report_final"
    

这两个方法在 3.9 版本引入,提供了更直观的前后缀删除功能。

2. 大小写转换

upper / lower / capitalize / swapcase / title

s = "Python3"
print(s.upper())      # "PYTHON3"
print(s.lower())      # "python3"
print(s.capitalize()) # "Python3"
print(s.swapcase())   # "pYTHON3"
print("hello world".title()) # "Hello World"
    

3. 判断类方法

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
    

4. 查找与计数

find / rfind / count

txt = "abracadabra"
print(txt.find("ra"))   # 2
print(txt.rfind("ra"))  # 9
print(txt.count("a"))   # 5
    

5. 前缀/后缀判断

s = "data_report.csv"
print(s.startswith("data_"))  # True
print(s.endswith(".csv"))      # True
    

6. 分割与拼接

split / rsplit / partition / rpartition

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')
    

join

words = ["Python", "is", "awesome"]
print(" ".join(words))   # "Python is awesome"
    

7. 替换与正则

replace

s = "I love Python. Python is great."
print(s.replace("Python", "Java"))  # "I love Java. Java is great."
    

re.sub(正则)

import re
s = "2025-11-20"
print(re.sub(r"-", "/", s))  # "2025/11/20"
    

8. 对齐与填充

s = "42"
print(s.zfill(5))   # "00042"
print(s.rjust(5, "*"))  # "***42"
print(s.ljust(5, "-"))  # "42---"
print(s.center(7, "~")) # "~42~~~"
    

9. 其他实用方法

10. 综合示例:读取文件、清洗、统计词频

# 读取文本文件,去除每行首尾空白,统计出现的单词数
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 个单词
    

以上示例覆盖了常用的字符串操作方法,帮助在实际项目中快速完成文本处理、数据清洗等任务。