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
相关推荐
froginwe1115 小时前
Python 循环嵌套
开发语言
咕白m62515 小时前
Excel 工作表名称读取(Python 实现)
后端·python
@大迁世界15 小时前
AI还替不了的JS能力
开发语言·前端·javascript·人工智能·ecmascript
ZJH__GO15 小时前
java项目-流水线线程池
java·开发语言
放逐者-保持本心,方可放逐15 小时前
Go + WebAssembly 构建树木数据统计分析系统
开发语言·golang·wasm·javascipt
godspeed_lucip16 小时前
LLM和Agent——专题5: LLM Ops 入门(1)
人工智能·python
ftpeak16 小时前
深入浅出 LoongSuite Python Agent:让你的 AI 应用「透明化」(下篇)
开发语言·人工智能·ai·ai编程·ai开发
希望永不加班16 小时前
SpringBoot 消息幂等性设计:防重复消费
java·开发语言·spring boot·后端·spring
l1t16 小时前
DeepSeek总结的使用实体-组件-系统和基于存在性处理进行Python编程7-8
开发语言·python
我是一颗柠檬16 小时前
【JDK8新特性】CompletableFuture异步编程Day10
java·开发语言·后端·intellij-idea