数据的验证是指程序对用户输入的数据进行”合法“性验证
一、 数据的验证的一些方法:
方法名 | 描述说明 |
str.isdigit() | 所有字符都是数字(阿拉伯数字) |
str.isnumeric() | 所有字符都是数字 |
str.isalpha() | 所有字符都是字母(包含中文字符) |
str.isalnum() | 所有字符都是数字或字母(包含中文字符) |
str.islower() | 所有字符都是小写 |
str.isupper() | 所有字符都是大写 |
str.istitle() | 所有字符都是首字母大写 |
str.isspace() | 所有字符都是空白字符(\n、\t等) |
注:都是通过字符串的形式验证的。
二、 数据验证操作的运用:
#isdigit()十进制的阿拉伯数字
print('123'.isdigit())#True
print('一二三'.isdigit())#False
print('0b1010'.isdigit())#False
print('ⅢⅢ'.isdigit())#False
print('-'*49)
print('123'.isnumeric())#True
print('一二三'.isnumeric())#True
print('0b1010'.isnumeric())#False
print('Ⅲ'.isnumeric())#True
print('壹贰叁'.isnumeric())#True
print('-'*49)
#所有字符都是字母(包含中文,就是把中文也看成字母,但是数字不可以)
print('hello你好'.isalpha())#True
print('hello你好123'.isalpha())#False
print('-'*49)
#所有字符都是字母或者是数字
print('hello你好'.isalnum())#True
print('hello你好123'.isalnum())#True
print('一二三'.isalnum())#True
print('ⅢⅢ'.isalnum())#True
print('壹贰叁'.isalnum())#True
print('-'*49)
#判断字母的大小写
print('HelloWorld'.islower())#False
print('helloworld'.islower())#True
print('helloworld你好世界'.islower())#True
print('HelloWorld'.isupper())#False
print('HELLOWORLD'.isupper())#True
print('HELLO你好'.isupper())#True#中文既是大写,也是小写。
print('-'*49)
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
#以上都为布尔类型
注:str.isnumeric()既能判断中文的数字要能判断罗马数字。
注:str.isalpha()中文字符也判断是字母,但是数字不能被判断为是。
注:str.isalnum()中文字符不影响其结果。
注中文字符既是大写字符也为小写字符。
注:以上结果都是布尔类型。