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
相关推荐
曲幽4 小时前
FastAPI + PostgreSQL 实战:从入门到不踩坑,一次讲透
python·sql·postgresql·fastapi·web·postgres·db·asyncpg
用户8356290780519 小时前
使用 C# 在 Excel 中创建数据透视表
后端·python
码路飞12 小时前
FastMCP 实战:一个 .py 文件,给 Claude Code 装上 3 个超实用工具
python·ai编程·mcp
dev派14 小时前
AI Agent 系统中的常用 Workflow 模式(2) Evaluator-Optimizer模式
python·langchain
前端付豪16 小时前
AI 数学辅导老师项目构想和初始化
前端·后端·python
用户03321266636716 小时前
将 PDF 文档转换为图片【Python 教程】
python
悟空爬虫17 小时前
UV实战教程,我啥要从Anaconda切换到uv来管理包?
python
dev派18 小时前
AI Agent 系统中的常用 Workflow 模式(1)
python·langchain
明月_清风20 小时前
从“能用”到“专业”:构建生产级装饰器与三层逻辑拆解
后端·python
曲幽1 天前
数据库实战:FastAPI + SQLAlchemy 2.0 + Alembic 从零搭建,踩坑实录
python·fastapi·web·sqlalchemy·db·asyncio·alembic