Python学习笔记(六):isalpha()函数的用法

isalpha()方法:判断字符串是否只由字母组成,如果字符串中所有字符都是字母则返回True,否则返回False。

str1 = "python"
print(str1.isalpha()) #True

# 中文的汉字会被isalpha判定为True
str2 = "我是一只大花猫"
print(str2.isalpha()) # True
# 如果想区分中文和英文可以使用unicode,中文的范围为:['/u4e00','/u9fa5']
print(str2.encode("utf-8").isalpha()) # False

str3 = "cat花猫"
print(str3.isalpha()) # True
print(str3.encode("utf-8").isalpha()) # False

猜你喜欢

转载自blog.csdn.net/weixin_44679832/article/details/113926623
今日推荐