分享丨Python 字符串检查方法完整大全
匿名用户
194
2025.07.14
2025.07.14
发布于 广东

一、基础字符属性检查

  1. 字母与数字检查
  •  isalpha() :判断字符串是否全为字母(含 Unicode 字母,如中文、希腊字母等)
    "abc".isalpha() → True
    "abc123".isalpha() → False
    "中文".isalpha() → True # 注意:中文在Python中被视为alpha字符
  •  isdigit() :判断是否全为数字字符(支持 Unicode 数字,如①、Ⅷ)
    "123".isdigit() → True
    "①②".isdigit() → True
    "12.3".isdigit() → False # 含小数点则为False
  •  isalnum() :判断是否只包含字母和数字(无其他字符)
    "abc123".isalnum() → True
    "abc_123".isalnum() → False # 含下划线则为False
  1. 数字细分检查
  •  isdecimal() :仅识别十进制数字字符(0-9)
    "123".isdecimal() → True
    "Ⅷ".isdecimal() → False # 罗马数字不识别
    "12.3".isdecimal() → False # 含小数点不识别
  •  isnumeric() :识别所有数字类字符(含中文数字、罗马数字等)
    "123".isnumeric() → True
    "三".isnumeric() → True # 中文数字
    "Ⅷ".isnumeric() → True # 罗马数字
    "12.3".isnumeric() → False # 含小数点不识别
  1. 大小写检查
  •  islower() :判断是否全为小写字母(非字母字符不影响)
    "hello".islower() → True
    "hello123".islower() → True
    "Hello".islower() → False
  •  isupper() :判断是否全为大写字母(非字母字符不影响)
    "WORLD".isupper() → True
    "WORLD123".isupper() → True
    "World".isupper() → False
  •  istitle() :判断是否为标题格式(每个单词首字母大写,其余小写)
    "Title Case".istitle() → True
    "Hello World".istitle() → True
    "hello World".istitle() → False
  1. 空白与特殊字符检查
  •  isspace() :判断是否全为空白字符(空格、\t、\n、\r 等)
    " ".isspace() → True
    "\t\n".isspace() → True
    " a ".isspace() → False # 含非空白字符
  •  isascii() :判断所有字符是否为 ASCII 字符(编码 0-127)
    "abc123".isascii() → True
    "中文".isascii() → False # 中文非ASCII
  •  isprintable() :判断是否全为可打印字符(不含 \t、\n 等控制字符)
    "abc123".isprintable() → True
    "abc\n".isprintable() → False # 含换行符
    "abc\t".isprintable() → False # 含制表符

二、字符串结构检查

  1. 首尾匹配检查
  •  startswith(prefix, start=0, end=len(s)) :检查是否以指定前缀开头(可指定检查范围)
    "hello.py".startswith("hello") → True
    "hello.py".startswith("py", 6) → True # 从索引6开始检查
  •  endswith(suffix, start=0, end=len(s)) :检查是否以指定后缀结尾(可指定检查范围)
    "data.csv".endswith(".csv") → True
    "data.csv".endswith("ta", 0, 4) → True # 检查前4个字符的结尾
  1. 子串存在检查
  •  in  运算符:判断子串是否存在于字符串中(最简洁方式)
    "py" in "python" → True
    "java" in "python" → False
  •  find(sub) :通过返回索引判断子串是否存在(-1 表示不存在)
    "python".find("py") → 0 # 存在,返回起始索引
    "python".find("java") → -1 # 不存在

三、进阶组合检查技巧

  1. 范围检查(结合切片)

s = "Abc123"
s[:1].isupper() # 检查首字符是否大写 → True
s[-3:].isdigit() # 检查最后3个字符是否为数字 → True

  1. 条件组合检查

验证密码:长度≥8,含大小写字母和数字

def check_pwd(pwd):
return (len(pwd) >= 8 and
any(c.isupper() for c in pwd) and
any(c.islower() for c in pwd) and
any(c.isdigit() for c in pwd))

  1. 正则表达式增强检查

import re

验证邮箱格式

def is_email(email):
pattern = r'^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+.[a-zA-Z0-9-.]+$'
return bool(re.match(pattern, email))

四、特殊注意事项

1. 空字符串:所有检查方法对空字符串均返回  False 
"".isalpha() → False
"".isdigit() → False

2. 混合字符影响: islower() / isupper()  只关注字母部分,非字母字符不影响结果
"hello!@#".islower() → True # 非字母字符不影响

3. Unicode 兼容性: isalpha()  会将中文等 Unicode 字母视为字母,使用时需注意场景
"中文".isalpha() → True # 可能不符合某些业务场景预期

五、常用场景示例

场景 实现方法示例
用户名验证  username.isalnum() and len(username)>=6 
手机号提取  ''.join(c for c in text if c.isdigit()) 
邮箱格式检查 结合正则表达式  re.match() 
文件名分类  filename.endswith(('.pdf', '.docx')) 

掌握这些方法,能轻松应对字符串验证、数据清洗、文本解析等场景,让代码更简洁高效!

评论 (0)