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
相关推荐
Goldn.8 小时前
Java核心技术栈全景解析:从Web开发到AI融合
java· spring boot· 微服务· ai· jvm· maven· hibernate
我一定会有钱8 小时前
斐波纳契数列、end关键字
python
玄斎9 小时前
MySQL 单表操作通关指南:建库 / 建表 / 插入 / 增删改查
运维·服务器·数据库·学习·程序人生·mysql·oracle
李慕婉学姐9 小时前
【开题答辩过程】以《基于Android的出租车运行监测系统设计与实现》为例,不知道这个选题怎么做的,不知道这个选题怎么开题答辩的可以进来看看
java·后端·vue
小鸡吃米…9 小时前
Python 列表
开发语言·python
m0_740043739 小时前
SpringBoot05-配置文件-热加载/日志框架slf4j/接口文档工具Swagger/Knife4j
java·spring boot·后端·log4j
编织幻境的妖10 小时前
SQL查询连续登录用户方法详解
java·数据库·sql
未若君雅裁10 小时前
JVM面试篇总结
java·jvm·面试
星依网络10 小时前
yolov5实现游戏图像识别与后续辅助功能
python·开源·游戏程序·骨骼绑定
kk哥889910 小时前
C++ 对象 核心介绍
java·jvm·c++