Python数据的验证

数据的验证是指程序对用户输入的数据进行'合法'性验证。

|----------------|----------------------|
| 方法名 | 描述说明 |
| str.isdigit() | 所有字符都是数字(阿拉伯数字) |
| str.isumeric() | 所有字符都是数字 |
| str.isalpha() | 所有字符都是字母(包含中文字符) |
| str.isalnum() | 所有字符都是数字或字母(包含中文字符) |
| str.islower() | 所有字符都是小写 |
| str.isupper() | 所有字符都是大写 |
| str.istitle() | 所有字符都是首字母大写 |
| str.isspace() | 所有字符都是空白字符(\n、\t等) |

python 复制代码
#isdigit()十进制的阿拉伯数字
print('123'.isdigit())#True
print('一二三'.isdigit())#False
print('0b1010'.isdigit())#False
print('Ⅲ'.isdigit())#False
print('*'*50)
#所有字符都是数字
print('123'.isnumeric())#True
print('一二三'.isnumeric())#True
print('0b1010'.isnumeric())#False
print('Ⅲ'.isnumeric())#True
print('壹'.isnumeric())#True
print('*'*50)
#所有字符都是字母
print('hello你好'.isalpha())#True
print('hello你好123'.isalpha())#False
print('hello你好一二三'.isalpha())#True
print('hello你好Ⅲ'.isalpha())#False
print('hello你好壹'.isalpha())#True
print('*'*50)
#所有字符都是数字或字母
print('hello你好'.isalnum())#True
print('hello你好123'.isalnum())#True
print('hello你好一二三'.isalnum())#True
print('hello你好Ⅲ'.isalnum())#True
print('hello你好壹'.isalnum())#True
print('*'*50)
#判断字符的大小写
print('HelloWorld'.islower())#False
print('helloworld'.islower())#True
print('hello你好'.islower())#True
print('*'*50)
print('HelloWorld'.isupper())#False
print('HELLOWORLD'.isupper())#True
print('HELLOWORLD你好'.isupper())#True
print('*'*50)
#判断首字母是否大写
print('Hello'.istitle())#True
print('HelloWorld'.istitle())#False
print('Helloworld'.istitle())#True
print('Hello World'.istitle())#True
print('Hello world'.istitle())#False
print('*'*50)
#判断是否都是空白字符
print('\t'.isspace())#True
print(' '.isspace())#True
print('\n'.isspace())#True
相关推荐
q5673152312 分钟前
IBM官网新闻爬虫代码示例
开发语言·分布式·爬虫
DanCheng-studio13 分钟前
毕设 基于机器视觉的驾驶疲劳检测系统(源码+论文)
python·毕业设计·毕设
carpell15 分钟前
【语义分割专栏】3:Segnet实战篇(附上完整可运行的代码pytorch)
人工智能·python·深度学习·计算机视觉·语义分割
笨笨马甲18 分钟前
附加模块--Qt OpenGL模块功能及架构
开发语言·qt
一只小波波呀1 小时前
打卡第48天
python
钮钴禄·爱因斯晨1 小时前
Java 面向对象进阶之多态:从概念到实践的深度解析
java·开发语言·数据结构
zstar-_1 小时前
一套个人知识储备库构建方案
python
鸽子炖汤1 小时前
Java中==和equals的区别
java·开发语言·jvm
有个傻瓜1 小时前
PHP语言核心技术全景解析
开发语言·kubernetes·php
Amo Xiang2 小时前
《100天精通Python——基础篇 2025 第5天:巩固核心知识,选择题实战演练基础语法》
python·选择题·基础语法