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
相关推荐
23516几秒前
【并发编程】详解volatile
java·开发语言·jvm·分布式·后端·并发编程·原理
GIS学姐嘉欣几秒前
【智慧城市】2025年中国地质大学(武汉)暑期实训优秀作品(5):智慧矿产
学习·gis·智慧城市·webgis
工业互联网专业6 分钟前
基于大数据的学习资源推送系统的设计与实现 _django
vue.js·python·django·毕业设计·源码·课程设计·学习资源推送系统
折翼的恶魔8 分钟前
前端学习之样式设计
前端·css·学习
洛小豆27 分钟前
java 中 char 类型变量能不能储存一个中文的汉字,为什么?
java·后端·面试
爱吃烤鸡翅的酸菜鱼33 分钟前
从数据库直连到缓存预热:城市列表查询的性能优化全流程
java·数据库·后端·spring·个人开发
一只学java的小汉堡1 小时前
Java 面试高频题:HashMap 与 ConcurrentHashMap 深度解析(含 JDK1.8 优化与线程安全原理)
java·开发语言·面试
huohaiyu2 小时前
Hashtable,HashMap,ConcurrentHashMap之间的区别
java·开发语言·多线程·哈希
信奥卷王3 小时前
[GESP202503 五级] 原根判断
java·数据结构·算法
木子杳衫3 小时前
【软件开发】管理类系统
python·web开发