python3字符串函数汇总

python版本:3.8.1

该版本下,str函数一共有44个,除去已经在别的文章中介绍过的format(参考链接),这里介绍剩下43个函数的用法。有任何错误欢迎在评论中指出。

isdigit

参数:无
用途:判断字符串是否全部由数字组成

>>> "12".isdigit()
True
>>> "a".isdigit()
False
>>> "".isdigit()
False

isalpha

参数:无
用途:判断字符串是否全部由字母组成

>>> "a".isalpha()
True
>>> "a1".isalpha()
False
>>> "".isalpha()
False

isalnum

参数:无
用途:判断字符串是否全部由字母或数字组成

>>> "a1".isalnum()
True
>>> "a1.".isalnum()
False
>>> "".isalnum()
False

isdecimal

参数:无
用途:判断字符串是否全部由十进制数字组成,和isdigit的区别在于,isdigit可用于byte类型,而isdecimal不可以

>>> b"1".isdigit()
True
>>> "1".isdecimal()
True
>>> b"1".isdecimal()
Traceback (most recent call last):
  File "<pyshell#14>", line 1, in <module>
    b"1".isdecimal()
AttributeError: 'bytes' object has no attribute 'isdecimal'
>>> "".isdecimal()
False

isnumeric

参数:无
用途:判断字符串是否全部由数字字符组成,和isdigit的区别在于,isdigit只能检测阿拉伯数字,isnumeric还可以检测汉字数字(大小写均可),罗马数字

>>> "1二叁".isnumeric()
True
>>> "1Ⅲ".isnumeric()
True
>>> "".isnumeric()
False

注意:上面例子中罗马数字是Ⅲ不是三个I

isupper

参数:无
用途:判断字符串是否全部由大写字母组成

>>> "ABC".isupper()
True
>>> "ABc".isupper()
False
>>> "".isupper()
False

islower

参数:无
用途:判断字符串是否全部由小写字母组成

>>> "abc".islower()
True
>>> "ABc".islower()
False
>>> "".islower()
False

isspace

参数:无
用途:判断字符串是否全部由空白字符组成

>>> " ".isspace()   #一个空格
True
>>> "	".isspace()   #一个制表符
True
>>> "\n".isspace()  #换行符
True
>>> "\r".isspace()  #回车符
True
>>> "".<
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值