栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python字符串函数系列(二)

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python字符串函数系列(二)

人生苦短,我用Python,坚持每日分享,今天再分享10个Python字符串函数!

1.isdecimal():
判断是否为数字,可以判断阿拉伯数字,判断不了字节数字和汉字数字

>>> n1 = '8'
>>> n2 = b'8'
>>> n3 = '八'
>>> n1.isdecimal()
True
>>> n2.isdecimal()
Traceback (most recent call last):
  File "", line 1, in 
AttributeError: 'bytes' object has no attribute 'isdecimal'
>>> n3.isdecimal()
False

2.isdigit():
判断是否为数字,可以判断阿拉伯数字和字节数字,判断不了汉字数字

>>> n1 = '8'
>>> n2 = b'8'
>>> n3 = '八'
>>> n1.isdigit()
True
>>> n2.isdigit()
True
>>> n3.isdigit()
False

3.isnumeric():
判断是否为数字,可以判断阿拉伯数字和汉字数字,判断不了阿拉伯数字

>>> n1 = '8'
>>> n2 = b'8'
>>> n3 = '八'
>>> n1.isnumeric()
True
>>> n2.isnumeric()
Traceback (most recent call last):
  File "", line 1, in 
AttributeError: 'bytes' object has no attribute 'isnumeric'
>>> n3.isnumeric()
True

4.isidentifier():
判断是否为合法的标识符

>>> s = '8apple'
>>> s.isidentifier()
False
>>> s = 'apple8'
>>> s.isidentifier()
True

5.islower():
判断字符串中的字母是否全部为小写字母,数字和其他符号不影响

>>> s = 'Abcde'
>>> s.islower()
False
>>> s = 'abcde'
>>> s.islower()
True
>>> s = 'abc34$'
>>> s.islower()
True

6.isascii():
判断是否全部为ASCII字符

>>> s = 'abcd568'
>>> s.isascii()
True
>>> s = 'abcd56哈哈'
>>> s.isascii()
False

7.isspace():
判断是否为空格

>>> s = ''
>>> s.isspace()
False
>>> s = ' '
>>> s.isspace()
True

8.istitle():
判断是否为有且仅有首字母为大写的字符串

>>> s = 'Abcdeg'
>>> s.istitle()
True
>>> s = 'abcdg'
>>> s.istitle()
False
>>> s = '123abc'
>>> s.istitle()
False
>>> s = 'AAAA'
>>> s.istitle()
False
>>> s = 'abcdgGG'
>>> s.istitle()
False
>>> s = 'AAbcd56'
>>> s.istitle()
False
>>> s = 'A123'
>>> s.istitle()
True

9.isupper():
判断字符串中的字母是否全部为大写字母,只要有一个小写字母就返回False,有数字和其他符号没关系

>>> s = 'ABCDE'
>>> s.isupper()
True
>>> s = 'abCDE'
>>> s.isupper()
False
>>> s = 'ABCD2'
>>> s.isupper()
True
>>> s = 'ABCD$'
>>> s.isupper()
True

10.isprintable():
判断是否为可打印的字符串

>>> s = b'67'
>>> s.isprintable()
Traceback (most recent call last):
  File "", line 1, in 
AttributeError: 'bytes' object has no attribute 'isprintable'
>>> s = '67ad'
>>> s.isprintable()
True

以上所有代码基于Python3.10测试

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/846310.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号