python里isalpha函数_Python isalpha函数判断字母

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')

  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值