Python判断字符串的构成

# istitle()
print("How Are You".istitle())  # True
print("how are you".istitle())  # False

# isspace   只有空格
print("   ".isspace())  # True
print(" 132  ".isspace())   # False

# isalpha 都是字母
print("abc".isalpha())  # True
print("123456".isalpha())  # False
print("123456abc".isalpha())  # False

# isupper 有至少一个大写字母
print("a123456".isupper())  # False
print("A123456".isupper())  # True

# isdigit 都是数字
print(b"123456".isdigit())  # True
print("123456abc".isdigit())  # False
print("asd".isdigit())  # False

# isdecimal 只包含十进制数字则返回
print("123000".isdecimal())  # True

# isnumeric 只包含数字字符,则返回 True,否则返回 False
print("123456".isnumeric())     # True
print("一二三四".isnumeric())     # True

# isdigit, isdecimal, isnumeric 区别
num = "1"  # unicode
num.isdigit()  # True
num.isdecimal()  # True
num.isnumeric()  # True

num = "1"  # 全角
num.isdigit()  # True
num.isdecimal()  # True
num.isnumeric()  # True

num = b"1"  # byte
num.isdigit()  # True
num.isdecimal()  # AttributeError 'bytes' object has no attribute 'isdecimal'
num.isnumeric()  # AttributeError 'bytes' object has no attribute 'isnumeric'

num = "IV"  # 罗马数字
num.isdigit()  # True
num.isdecimal()  # False
num.isnumeric()  # True

num = "四"  # 汉字
num.isdigit()  # False
num.isdecimal()  # False
num.isnumeric()  # True


# 判断有汉字
import re
chinese_str = "中国人民银行"
search_ret = re.compile(u'[\u4e00-\u9fa5]')
ret = search_ret.search(chinese_str)
if ret:
    print("有汉字")
else:
    print("无汉字")
发布了73 篇原创文章 · 获赞 14 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/qq_42327755/article/details/103563640