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

python判断字符串是否包含字母的方法:

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

#-*-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)

以上内容为大家介绍了python培训之判断字符串是否包含字母,希望对大家有所帮助,如果想要了解更多Python相关知识,请关注我们

推荐阅读