Python isalpha() 方法检测字符串是否只由字母组成。
Case 1: 纯字母
info = 'abcd'
print(info.isalpha())
返回结果是True
Case 2: 含汉字
info = 'abcd哈哈哈'
print(info.isalpha())
结果返回True,说明在python语言里 汉字是当做字母处理的
Case 3: 含数字或空格
info = 'abcd 124'
print(info.isalpha())
结果返回False
Case 4: 含特殊符号
info = 'abcd!@#$^&'
print(info.isalpha())
结果返回False
延伸:
判断字符串为纯英文字母:
加上属性encode('UTF-8')
info = 'abcd'
print(info.encode('UTF-8').isalpha())
判断字符串为纯中文字母:
Unicdoe4E00~9FFF表示中文,所以如果一个字符的utf-8编码在这个区间内,就说明它是中文:
def is_Chinese(word):
for ch in word:
if 'u4e00' <= ch <= 'u9fff':
print('This is Chinese')
else :
print('This is not Chinese')
is_Chinese('哈哈哈哈123')