Python3 笔记:字符串的 isspace()、istitle()、isdecimal()

1、isspace() 方法检测字符串是否只由空白字符组成。

语法:str.isspace()

如果字符串中只包含空格,则返回 True,否则返回 False。

python 复制代码
str1 = '    '
str2 = 'hello '
print(str1.isspace())	# 运行结果:True
print(str2.isspace())	# 运行结果:False

2、istitle() 方法检测字符串中所有的单词拼写首字母是否为大写,且其他字母为小写。

语法:str.istitle()

如果字符串中所有的单词拼写首字母是否为大写,且其他字母为小写则返回 True,否则返回 False。

python 复制代码
str1 = 'A Title'
str2 = 'This is not a Title'
print(str1.istitle())	# 运行结果:True
print(str2.istitle())	# 运行结果:False

3、isdecimal() 方法检查字符串是否只包含十进制字符。

语法:str.isdecimal()

如果字符串中的所有字符都是十进制字符则返回True,否则返回False。

python 复制代码
str1 = '12345'
str2 = 'Number12345'
print(str1.isdecimal())	# 运行结果:True
print(str2.isdecimal())	# 运行结果:False
相关推荐
安_2 小时前
如何构建和使用向量索引?HNSW 和 IVF 有什么区别?
python·ai
counting money3 小时前
Java IO流详解:从InputStream到文件操作实战
java·开发语言·python
坚持学习前端日记4 小时前
Python SQLAlchemy ORM 从0到1精通实战手册(基础到复杂高阶)
数据库·python·oracle
峥无4 小时前
从0到1手撕红黑树:封装实现 my_map 与 my_set(SGI-STL 源码级深度解析)
开发语言·c++·笔记·算法·stl
北斗落凡尘4 小时前
LangGraph 入门实战(11)--输出模式
后端·python·langchain
不会代码的小猴5 小时前
3. 控件学习1
开发语言·c++·笔记·qt·算法
雪碧聊技术5 小时前
安装Python(保姆级教程)
python·版本更新·python下载
++==5 小时前
JSON和Python的 四种核心容器
python·json
疯狂打码的少年6 小时前
【数据结构】图的存储结构:邻接矩阵与邻接表
数据结构·笔记
蒸蒸yyyyzwd7 小时前
cpp 选手准备秋招学习笔记 day10
笔记·面试·求职招聘