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
相关推荐
平凡的小码农2 分钟前
JAVA实现大写金额转小写金额
java·开发语言
一眼万里*e12 分钟前
fish-speech语音大模型本地部署
python·flask·大模型
yttandb16 分钟前
重生到现代之从零开始的C语言生活》—— 内存的存储
c语言·开发语言·生活
我明天再来学Web渗透20 分钟前
【hot100-java】【二叉树的层序遍历】
java·开发语言·数据库·sql·算法·排序算法
结衣结衣.34 分钟前
python中的函数介绍
java·c语言·开发语言·前端·笔记·python·学习
茫茫人海一粒沙37 分钟前
Python 代码编写规范
开发语言·python
原野心存37 分钟前
java基础进阶知识点汇总(1)
java·开发语言
林浩23338 分钟前
Python——异常处理机制
python
程序猿阿伟40 分钟前
《C++高效图形用户界面(GUI)开发:探索与实践》
开发语言·c++
暗恋 懒羊羊1 小时前
Linux 生产者消费者模型
linux·开发语言·ubuntu