周志洋

个人站

持续学习 才能不被淘汰


Python实用技巧-字符串与正则

字符串格式化推荐

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实用技巧-字符串与正则

打赏一个呗

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦