当前位置:首页 > 云计算 > 正文内容

python判断字符串是否包含字母

2022-05-04 03:00:18云计算3

第一种方法:使用正则表达式判断字符串是否包含字母

#-*-coding:utf-8-*-importre
defcheck(str):
my_re=re.compile(r'[A-Za-z]',re.S)
res=re.findall(my_re,str)
iflen(res):
printu'含有英文字符'
else:
printu'不含有英文字符'if__name__=='__main__':
str='你好123hello'
check(str)
str1='你好123'
check(str1)

第二种方法:使用isalpha()。是字母的时候返回True,不是字母的时候返回False,

#-*-coding:utf-8-*-defcheck(str):
str_1=list(str)
foriinstr_1:
ifi.isalpha():
print'*'*15
printu'含有英文字符'
breakif__name__=='__main__':
str='你好123'
check(str)
#*********************************
str1='你好123helloworld'
check(str1)

本网站文章仅供交流学习 ,不作为商用, 版权归属原作者,部分文章推送时未能及时与原作者取得联系,若来源标注错误或侵犯到您的权益烦请告知,我们将立即删除.

本文链接:https://www.xibujisuan.cn/2935.html

标签: Python