在Python中,判断一个字符是否是英文字母,可以使用以下几种方法:
1. 使用 `isalpha()` 方法:
char = 'a'
print(char.isalpha()) 输出:True
2. 使用 `ord()` 函数和 ASCII 码范围判断:
char = 'a'
print(65 <= ord(char) <= 90 or 97 <= ord(char) <= 122) 输出:True
3. 使用正则表达式判断:
import re
def check_alpha(string):
pattern = re.compile(r'[A-Za-z]')
return bool(pattern.search(string))
str_with_alpha = 'Hello World'
str_without_alpha = ''
print(check_alpha(str_with_alpha)) 输出:True
print(check_alpha(str_without_alpha)) 输出:False