1. 使用内置函数 `isalpha()`:
str1 = 'geek-docs.com'str2 = '123'print(str1.isalpha()) Trueprint(str2.isalpha()) False
2. 使用正则表达式:
import redef check(str):my_re = re.compile(r'[A-Za-z]', re.S)res = re.findall(my_re, str)if len(res):print('含有英文字符')else:print('不含有英文字符')str = '你好123hello'check(str)str1 = '你好123'check(str1)
3. 使用ASCII码判断:
char1 = 'a'char2 = '1'print(65 <= ord(char1) <= 90 or 97 <= ord(char1) <= 122) Trueprint(65 <= ord(char2) <= 90 or 97 <= ord(char2) <= 122) False
以上方法可以帮助你识别字符串中的英文字母。

