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
相关推荐
xinxiyinhe18 分钟前
如何设置Cursor中.cursorrules文件
人工智能·python
gz927cool24 分钟前
大模型做导师之开源项目学习(lightRAG)
学习·开源·mfc
风与沙的较量丶28 分钟前
Java中的局部变量和成员变量在内存中的位置
java·开发语言
m0_7482517229 分钟前
SpringBoot3 升级介绍
java
诸神缄默不语44 分钟前
如何用Python 3自动打开exe程序
python·os·subprocess·python 3
橘子师兄1 小时前
分页功能组件开发
数据库·python·django
Logintern091 小时前
使用VS Code进行Python编程的一些快捷方式
开发语言·python
Multiple-ji2 小时前
想学python进来看看把
开发语言·python
极客先躯2 小时前
说说高级java每日一道面试题-2025年2月13日-数据库篇-请说说 MySQL 数据库的锁 ?
java·数据库·mysql·数据库的锁·模式分·粒度分·属性分
程序员侠客行2 小时前
Spring事务原理 二
java·后端·spring