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
相关推荐
万邦科技Lafite39 分钟前
京东按图搜索京东商品(拍立淘) API (.jd.item_search_img)快速抓取数据
开发语言·前端·数据库·python·电商开放平台·京东开放平台
丁浩6661 小时前
Python机器学习---6.集成学习与随机森林
python·随机森林·机器学习
默 语2 小时前
MySQL中的数据去重,该用DISTINCT还是GROUP BY?
java·数据库·mysql·distinct·group by·1024程序员节·数据去重
wdfk_prog2 小时前
[Linux]学习笔记系列 -- [kernel][time]alarmtimer
linux·笔记·学习
charlie1145141912 小时前
现代 Python 学习笔记:Statements & Syntax
笔记·python·学习·教程·基础·现代python·python3.13
ha20428941942 小时前
Linux操作系统学习之---基于环形队列的生产者消费者模型(毛坯版)
linux·c++·学习
dxnb222 小时前
Datawhale25年10月组队学习:math for AI+Task5解析几何
人工智能·学习
哲Zheᗜe༘3 小时前
了解学习Redis主从复制
数据库·redis·学习
oDeviloo3 小时前
新版IntelliJ IDEA个性化设置兼容老版习惯
java·ide·intellij-idea
一只小透明啊啊啊啊3 小时前
Java Web 开发的核心组件:Servlet, JSP,Filter,Listener
java·前端·servlet