python学习——数据的验证

文章目录

      • [1. `str.isdigit()`](#1. str.isdigit())
      • [2. `str.isnumeric()`](#2. str.isnumeric())
      • [3. `str.isalpha()`](#3. str.isalpha())
      • [4. `str.isalnum()`](#4. str.isalnum())
      • [5. `str.islower()`](#5. str.islower())
      • [6. `str.isupper()`](#6. str.isupper())
      • [7. `str.istitle()`](#7. str.istitle())
      • [8. `str.isspace()`](#8. str.isspace())
      • 实操


以下是Python中字符串数据验证方法的详细解释:

1. str.isdigit()

这个方法用于检查字符串中的所有字符是否都是数字。它只对阿拉伯数字(0-9)有效。如果字符串至少包含一个非数字字符,它将返回False

python 复制代码
"12345".isdigit()  # 返回 True
"123a45".isdigit()  # 返回 False

2. str.isnumeric()

isdigit()类似,isnumeric()也检查字符串是否只包含数字字符。但是,isnumeric()可以识别某些特殊字符(如罗马数字和汉字数字),这些字符在某些情况下也被视为数字。

python 复制代码
"12345".isnumeric()  # 返回 True
"一二三四五".isnumeric()  # 返回 True,因为这些都是汉字数字

3. str.isalpha()

这个方法用于检查字符串中的所有字符是否都是字母。它适用于所有语言的字母字符,包括中文。

python 复制代码
"abcdef".isalpha()  # 返回 True
"abc123".isalpha()  # 返回 False
"汉字".isalpha()  # 返回 True

4. str.isalnum()

isalnum()方法检查字符串是否只包含字母和数字。这意味着字符串不能包含任何特殊字符或空格。

python 复制代码
"abc123".isalnum()  # 返回 True
"abc 123".isalnum()  # 返回 False,因为包含了空格
"汉字123".isalnum()  # 返回 True

5. str.islower()

这个方法用于检查字符串中的所有字符是否都是小写字母。

python 复制代码
"hello".islower()  # 返回 True
"Hello".islower()  # 返回 False,因为包含了大写字母

6. str.isupper()

islower()相反,isupper()检查字符串中的所有字符是否都是大写字母。

python 复制代码
"HELLO".isupper()  # 返回 True
"Hello".isupper()  # 返回 False,因为包含了小写字母

7. str.istitle()

istitle()方法检查字符串是否是标题化的,即每个单词的首字母都是大写,其余字母都是小写。

python 复制代码
"Hello World".istitle()  # 返回 True
"hello world".istitle()  # 返回 False,因为没有单词首字母大写

8. str.isspace()

这个方法检查字符串中的所有字符是否都是空白字符,如空格、制表符(\t)、换行符(\n)等。

python 复制代码
"   ".isspace()  # 返回 True
"\t\n".isspace()  # 返回 True
" a ".isspace()  # 返回 False,因为包含了非空白字符

这些方法在Python中用于快速验证字符串内容是否符合特定的格式要求。它们在处理用户输入、数据清洗和格式化等场景中非常有用。需要注意的是,这些方法都是针对字符串类型的,如果用于其他数据类型,将会引发AttributeError

实操

python 复制代码
# isdigit()是否为10进制的阿拉伯数字
print('123'.isdigit())
print('一二三'.isdigit())
print('0b1010'.isdigit())
print('-'*50)

# 所有的字符都是数字吗
print('123'.isnumeric())
print('一二三'.isnumeric())  # true!
print('0b1010'.isnumeric())
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('壹贰叁'.isalpha())      # 这个也属于字母
print('-'*50)

# 所有字符都是数字或字母(有数字或字母都可以)
print('hello你好'.isalnum())
print('hello你好123'.isalnum())
print('hello你好一二三'.isalnum())
print('hello你好ⅠⅡⅢ'.isalnum())
print('-'*50)

# 判断字符的大小写
print('HelloWorld'.islower())
print('helloworld'.islower())
print('hello你好'.islower())
print()
print('HelloWorld'.isupper())
print('helloworld'.isupper())
print('hello你好'.isupper())  # 认为中文既是大写也是小写。。。
print('-'*50)

# 判断所有字符都是首字母大写
print('Hello'.istitle())  # True
print('HelloWorld'.istitle())  # 为什么是false,因为W不是首字母,应该小写,所有报错
print('Helloworld'.istitle())  # True
print('Hello World'.istitle())  # True
print('helloWorld'.istitle())  # False
print('-'*50)

# 判断是否都是空白字符
print('\t'.isspace())  # True
print(' '.isspace())  # True
print('\n'.isspace())  # True
相关推荐
青瓷程序设计20 分钟前
动物识别系统【最新版】Python+TensorFlow+Vue3+Django+人工智能+深度学习+卷积神经网络算法
人工智能·python·深度学习
励志成为美貌才华为一体的女子21 分钟前
强化学习PPO和GRPO逻辑学习
学习
tobebetter952722 分钟前
How to manage python versions on windows
开发语言·windows·python
F_D_Z1 小时前
数据集相关类代码回顾理解 | sns.distplot\%matplotlib inline\sns.scatterplot
python·深度学习·matplotlib
meichaoWen1 小时前
【Vue3】vue3的全面学习(一)
前端·javascript·学习
FFF团团员9091 小时前
树莓派学习笔记3:LED和Button
笔记·学习
Y***h1871 小时前
第二章 Spring中的Bean
java·后端·spring
8***29311 小时前
解决 Tomcat 跨域问题 - Tomcat 配置静态文件和 Java Web 服务(Spring MVC Springboot)同时允许跨域
java·前端·spring
daidaidaiyu1 小时前
一文入门 LangGraph 开发
python·ai
CoderYanger1 小时前
优选算法-栈:67.基本计算器Ⅱ
java·开发语言·算法·leetcode·职场和发展·1024程序员节