鱼C论坛

 找回密码
 立即注册
查看: 2116|回复: 2

[已解决]isalnum()方法的一个疑惑

[复制链接]
发表于 2017-5-27 11:29:04 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
>>>str1 = '中文'
>>>str1.isalnum()
True


isalnum()函数的定义是:如果字符串至少有一个字符并且所有字符都是字母或数字则返回True,否则返回False
那这里str1赋值为中文字符,使用isalnum()方法后返回True,这是为什么呢?
最佳答案
2017-5-27 15:31:47
你查到的回答是没问题的。
isalnum()这个方法在几种数据类型中都有定义:
如果字符串是纯ascii码中的字符,那就只有字母和数字的情况下才会为True。
bytearray.isalnum()
Return true if all bytes in the sequence are alphabetical ASCII characters or ASCII decimal digits and the sequence is not empty, false otherwise. Alphabetic ASCII characters are those byte values in the sequence b'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'. ASCII decimal digits are those byte values in the sequence b'0123456789'.

如果字符串还包含其他编码下的字符,那就按照你2楼中所说的那样判断。而且你看在说明文档中,判断条件也改为了“alphanumeric”
str.isalnum()
Return true if all characters in the string are alphanumeric and there is at least one character, false otherwise. A character c is alphanumeric if one of the following returns True: c.isalpha(), c.isdecimal(), c.isdigit(), or c.isnumeric().

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2017-5-27 11:30:36 | 显示全部楼层
百度得到一个相关回答:
对于unicode string,string.isalpha会根据字符串中的字符是否属于Unicode编码的LETTER区域来判断是否都由字母组成。所以得出的结果为True,不一定表示只有26个英文字母。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-5-27 15:31:47 | 显示全部楼层    本楼为最佳答案   
你查到的回答是没问题的。
isalnum()这个方法在几种数据类型中都有定义:
如果字符串是纯ascii码中的字符,那就只有字母和数字的情况下才会为True。
bytearray.isalnum()
Return true if all bytes in the sequence are alphabetical ASCII characters or ASCII decimal digits and the sequence is not empty, false otherwise. Alphabetic ASCII characters are those byte values in the sequence b'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'. ASCII decimal digits are those byte values in the sequence b'0123456789'.

如果字符串还包含其他编码下的字符,那就按照你2楼中所说的那样判断。而且你看在说明文档中,判断条件也改为了“alphanumeric”
str.isalnum()
Return true if all characters in the string are alphanumeric and there is at least one character, false otherwise. A character c is alphanumeric if one of the following returns True: c.isalpha(), c.isdecimal(), c.isdigit(), or c.isnumeric().

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-6-10 09:47

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表