Python字符串isidentifier()

Python String isidentifier() function returns True if the string is a valid identifier according to the Python language definition.

python string isidentifier()

如果字符串是根据Python语言定义的有效标识符 ,则Python String isidentifier()函数返回True

Python字符串isidentifier() (Python String isidentifier())

A valid identifier string can be of any length. Prior to Python 3.0, a valid identifier can contain uppercase and lowercase letters A through Z, the underscore _ and, except for the first character, the digits 0 through 9.

有效的标识符字符串可以是任何长度。 在Python 3.0之前,有效的标识符可以包含大写和小写字母A到Z,下划线_以及除第一个字符外的数字0到9。

However, Python 3.0 introduced additional characters from outside the ASCII range that can be used to create an identifier. This change was done against PEP-3131.

但是,Python 3.0引入了ASCII范围之外的其他字符,可用于创建标识符。 针对PEP-3131进行了此更改。

Let’s look at some of the examples of Python String isidentifier() function.

我们来看一些Python String isidentifier()函数的示例。

s = 'xyzABC'
print(f'{s} is a valid identifier = {s.isidentifier()}')

Output: xyzABC is a valid identifier = True

输出: xyzABC is a valid identifier = True

s = '0xyz'
print(f'{s} is a valid identifier = {s.isidentifier()}')

Output: 0xyz is a valid identifier = False because an identifier can’t start with digits 0-9.

输出: 0xyz is a valid identifier = False因为标识符不能以数字0-9开头。

s = ''
print(f'{s} is a valid identifier = {s.isidentifier()}')

Output: is a valid identifier = False because an identifier can’t be empty string.

输出: is a valid identifier = False因为标识符不能为空字符串。

s = '_xyz'
print(f'{s} is a valid identifier = {s.isidentifier()}')

Output: _xyz is a valid identifier = True because underscore is allowed to be first character in the identifier string.

输出: _xyz is a valid identifier = True因为在标识符字符串中允许下划线成为第一个字符。

s = 'ꝗꞨꫳ'
print(f'{s} is a valid identifier = {s.isidentifier()}')

Output: ꝗꞨꫳ is a valid identifier = True

输出: ꝗꞨꫳ is a valid identifier = True

It’s a valid identifier because of PEP-3131 that added these additional Non-ASCII characters to the valid identifier character list. However, if you are using Python 2.x then it will return False.

由于PEP-3131会将这些其他非ASCII字符添加到有效标识符字符列表中,因此它是有效的标识符。 但是,如果您使用的是Python 2.x,则它将返回False

打印所有有效的标识符字符列表 (Print all valid identifier characters list)

We can use unicodedata to check if a character is a part of valid identifiers list or not. Here is the program to print all the valid characters that can be used to create an identifier.

我们可以使用unicodedata来检查字符是否是有效标识符列表的一部分。 这是打印所有可用于创建标识符的有效字符的程序。

import unicodedata

count = 0
for codepoint in range(2 ** 16):
    ch = chr(codepoint)
    if ch.isidentifier():
        print(u'{:04x}: {} ({})'.format(codepoint, ch, unicodedata.name(ch, 'UNNAMED')))
        count = count + 1
print(f'Total Number of Identifier 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 Identifier Unicode Characters = 48880

Note that I am providing only a few characters in the output because the valid identifier characters count is huge.

请注意,由于有效的标识符字符数量巨大,因此我在输出中仅提供了几个字符。

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

Reference: Official Documentation, PEP-3131

参考: 官方文档PEP-3131

翻译自: https://www.journaldev.com/24059/python-string-isidentifier

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值