Python字符串isalnum()

Python string isalnum() function returns True if it’s made of alphanumeric characters only. A character is alphanumeric if it’s either an alpha or a number.

如果Python字符串isalnum()函数仅由字母数字字符组成,则返回True 。 如果字符是字母或数字,则为字母数字

If the string is empty, then isalnum() returns False.

python string isalnum()

如果字符串为空,则isalnum()返回False

Python字符串isalnum()示例 (Python string isalnum() example)

s = 'HelloWorld2019'
print(s.isalnum())

Output: True

输出: True

s = 'Hello World 2019'

print(s.isalnum())

Output: False because whitespace is not an alphanumeric character.

输出: False因为空格不是字母数字字符。

s = ''
print(s.isalnum())

Output: False because it’s an empty string.

输出: False因为它是一个空字符串。

s='A.B'
print(s.isalnum())

s = '10.50'
print(s.isalnum())

Output:

输出:

False
False

The string contains period (.) which is not an alphanumeric character.

该字符串包含句点(。),它不是字母数字字符。

s = 'çåøÉ'
print(s.isalnum())

Output: True because all these are Alpha characters. Alphabetic characters are those characters defined in the Unicode character database as “Letter”, i.e., those with general category property being one of “Lm”, “Lt”, “Lu”, “Ll”, or “Lo”.

输出: True因为所有这些都是Alpha字符。 字母字符是在Unicode字符数据库中定义为“字母”的那些字符,即,具有一般类别属性为“ Lm”,“ Lt”,“ Lu”,“ Ll”或“ Lo”之一的那些字符。

在Python中打印所有字母数字字符 (Printing all Alphanumeric characters in Python)

We can use unicode module to check if a character is alphanumeric or not. Here is the program to print all the alphanumeric unicode characters.

我们可以使用unicode模块来检查字符是否为字母数字。 这是打印所有字母数字Unicode字符的程序。

import unicodedata

count = 0
for codepoint in range(2 ** 16):
    ch = chr(codepoint)
    if ch.isalnum():
        print(u'{:04x}: {} ({})'.format(codepoint, ch, unicodedata.name(ch, 'UNNAMED')))
        count = count + 1
print(f'Total Number of Alphanumeric Unicode Characters = {count}')

Output:

输出:

...
ffd7: ᅲ (HALFWIDTH HANGUL LETTER YU)
ffda: ᅳ (HALFWIDTH HANGUL LETTER EU)
ffdb: ᅴ (HALFWIDTH HANGUL LETTER YI)
ffdc: ᅵ (HALFWIDTH HANGUL LETTER I)
Total Number of Alphanumeric Unicode Characters = 49567

I have provided only partial output because the number of alphanumeric unicode characters is huge.

我只提供了部分输出,因为字母数字Unicode字符的数量很大。

GitHub Repository. GitHub存储库中签出更多Python示例。

Reference: Official Documentation

参考: 官方文档

翻译自: https://www.journaldev.com/24017/python-string-isalnum

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值