python 判断字符串中字符类型的常用方法

python 判断字符串中字符类型组成常用的方法

  • startswith()

检查字符串是否以设定内容开头,默认是整个字符串,如果是则返回True 否则则返回False

str1='hello'
print(str1.startswith('he'))  # True
print(str1.startswith('ol'))  # False
  • endswith()

检查字符串是否以设定内容结尾,默认是整个字符串,如果是则返回True 否则则返回False

str1='hello'
print(str1.endswith('o'))   # True
print(str1.endswith('h'))   # False
  • isdigit()

检查是否所有字符都为:数字0~9、罗马数字(小数点和正负号视作非数字)

print('3.1451'.isdigit())  # False
print('123456'.isdigit())  # True
print('125aa'.isdigit())   # False
  • isspace()

检查是否所有字符都为空白 ‘\t’

print('     \t'.isspace())   # True
print('       '.isspace())   # True
print('h    0'.isspace())    # False
  • isalnum()

判断是否所有字符都是字母或数字,是则返回 True,否则返回 False

print('123'.isalnum())   # True
print('abc'.isalnum())    # True
print('abc123'.isalnum())   # True
print('abc123_#'.isalnum())   # False
  • isalpha()

判断字符串是否只由字母组成,如果只有字母返回True,否则返回False

print('1234'.isalpha())  # False
print('adc'.isalpha())   # True
print('123adc'.isalpha())  # False
  • islower()

判断字符串是否由小写字母和其他字符组成,如果字符串里没有大写字母返回True,否则返回False

print('abcd'.islower())  # True
print('Abcd'.islower())  # False
print('abcd123-#'.islower())  # True
print('abcD123'.islower())  # False
  • istitle()

判断字符串中所有的单词拼写首字母是否为大写,且其他字母为小写。

print('Hello'.istitle())   # True
print('Hello 123'.istitle())  # True
print('HeLLo'.istitle())  # False
  • isupper()

判断字符串中所有的字母是否都为大写

print('HELLO'.isupper())     # True
print('HELLO 123'.isupper())  # True
print('Hello'.isupper())   # False
  • 8
    点赞
  • 48
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值