字符串格式化推荐
name, score = "Alice", 95 print(f"{name} scored {score}") # f-string print("{name} scored {score}".format(name=name, score=score))
常用字符串方法
s = " hello,Python " s = s.strip().lower().replace(",", ", ") parts = s.split()
正则表达式基础
import re m = re.search(r"(\d{4})-(\d{2})-(\d{2})", "2019-03-08") if m: year, month, day = m.groups()
常见场景
- 匹配邮箱:
[\w.-]+@[\w.-]+\.[A-Za-z]{2,}
- 提取数字:
r"[-+]?\d+(?:\.\d+)?"
转载请注明:周志洋的博客 » Python实用技巧-字符串与正则