Python 字符串:处理文本的这几十种方法,其实常用的就那些
写 Python 代码,字符串操作避不开。解析日志、处理用户输入、拼 SQL、格式化输出——天天都在跟文本打交道。
Python 的字符串方法多,但常用的就那么十几个。
字符串是什么,记住两点就够了
1 2 3 4 5
| s = "hello"
s = "H" + s[1:]
|
字符串一旦创建就不能改,所有”修改”操作都是创建新字符串。记住这点,写代码的时候就不会困惑了。
1 2 3 4 5
| s = "Python" s[0] s[-1] s[1:4] s[::-1]
|
切片是字符串最常用的操作之一,记住 [start:stop:step] 左闭右开就行。
查和找:在一段文本里找东西
判断开头结尾:
1 2 3 4
| url = "https://example.com"
url.startswith("https") url.endswith(".com")
|
找子串的位置:
1 2 3 4 5 6 7 8
| text = "hello world, hello python"
text.find("world") text.find("java") text.rfind("hello")
text.index("world")
|
日常用 find 就够了,返回 -1 表示没有。index 会抛异常,除非你很确定那个子串一定存在。
统计出现次数:
1 2
| text.count("hello") text.count("o")
|
检查是否包含某个子串:
1 2 3 4 5
| if "world" in text: print("找到了")
if "java" not in text: print("没有")
|
in 和 not in 比 find() != -1 更直观,推荐用这个。
改:替换、大小写、去空格
替换:
1 2 3
| text = "hello world, hello python" text.replace("hello", "hi") text.replace("hello", "hi", 1)
|
大小写转换:
1 2 3 4 5 6
| s = "Hello World"
s.upper() s.lower() s.capitalize() s.title()
|
去空格和换行:
1 2 3 4 5 6 7
| s = " \t hello \n " s.strip() s.lstrip() s.rstrip()
"***hello***".strip("*")
|
处理用户输入的时候,strip() 几乎是必用的——去掉用户不小心敲的空格。
拆和拼:字符串 ↔ 列表
拆:字符串变列表
1 2 3 4 5 6 7 8 9 10 11
| s = "apple,banana,orange" s.split(",")
"hello world".split()
"a,b,c,d".split(",", 2)
"a\nb\nc".splitlines()
|
拼:列表变字符串
1 2 3
| fruits = ['apple', 'banana', 'orange'] ",".join(fruits) " ".join(fruits)
|
拼大量字符串的时候,千万别用 +=:
1 2 3 4 5 6 7 8 9 10
| result = "" for i in range(10000): result += str(i)
parts = [] for i in range(10000): parts.append(str(i)) result = "".join(parts)
|
join 比 += 快 10 到 50 倍,数据量大的时候差距更明显。
格式化:把变量放进字符串
三种方式,我建议直接用第三种。
方式1:% 格式化(老式,不推荐)
1
| "姓名:%s,年龄:%d" % ("张三", 25)
|
1
| "姓名:{},年龄:{}".format("张三", 25)
|
方式3:f-string(Python 3.6+,推荐)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| name = "张三" age = 25 f"姓名:{name},年龄:{age}"
f"明年年龄:{age + 1}"
f"价格:{price:.2f}" f"百分比:{rate:.1%}"
f"{"左":<10}" f"{"右":>10}" f"{"中":^10}"
|
f-string 比前两种都快,而且代码最干净。能用 f-string 就别用前面两种了。
判断:检查字符串是什么类型
1 2 3 4 5 6
| "123".isdigit() "abc".isalpha() "abc123".isalnum() " ".isspace() "Hello".islower() "HELLO".isupper()
|
这些方法在校验用户输入的时候很好用:
1 2 3
| phone = input("请输入手机号:") if not phone.isdigit(): print("手机号只能包含数字")
|
正则表达式:复杂匹配的终极武器
字符串方法只能做简单匹配,遇到复杂需求就得用正则了。
提取手机号:
1 2 3 4 5 6
| import re
text = "我的电话是 13812345678" match = re.search(r"1[3-9]\d{9}", text) if match: print(match.group())
|
提取所有邮箱:
1 2 3
| text = "联系我:test@example.com 或 admin@test.com" emails = re.findall(r"\w+@\w+\.\w+", text)
|
替换敏感信息:
1
| re.sub(r"1[3-9]\d{9}", "***", text)
|
常用正则写法速查:
| 需求 |
正则 |
| 手机号 |
1[3-9]\d{9} |
| 邮箱 |
\w+@\w+\.\w+ |
| 数字 |
\d+ |
| 中文 |
[\u4e00-\u9fa5]+ |
| URL |
https?://[^\s]+ |
匹配中文的时候 \w 在 Python 里默认只匹配 ASCII,要匹配中文得用 [\u4e00-\u9fa5]。