Python字符串isidentifier()

本文详细介绍了Python中的isidentifier()函数,用于判断字符串是否为合法的标识符。文章通过多个示例解释了Python3.0及以后版本中标识符的规则变化,包括非ASCII字符的引入,并展示了如何使用unicodedata模块打印所有有效的标识符字符。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

1. capitalize(): 将字符串的第一个字符转换为大写字母。 2. casefold(): 将字符串中的所有字符转换为小写字母。与lower()方法类似,但是对于某些特殊字符的处理不同。 3. center(width[, fillchar]): 将字符串居中,并在两侧填充指定的字符(默认为空格)。 4. count(sub[, start[, end]]): 返回字符串中子字符串sub出现的次数。可指定起始和结束位置。 5. encode(encoding='utf-8', errors='strict'): 将字符串编码为指定的编码格式,返回一个bytes对象。 6. endswith(suffix[, start[, end]]): 判断字符串是否以指定的后缀结尾。可指定起始和结束位置。 7. expandtabs(tabsize=8): 将字符串中的制表符(\t)替换为指定数量的空格(默认为8)。 8. find(sub[, start[, end]]): 在字符串中查找子字符串sub,并返回其第一次出现的位置。可指定起始和结束位置。 9. format(*args, **kwargs): 格式化字符串,将字符串中的占位符替换为指定的值。 10. format_map(mapping): 格式化字符串,将字符串中的占位符替换为字典中对应的值。 11. index(sub[, start[, end]]): 在字符串中查找子字符串sub,并返回其第一次出现的位置。与find()方法类似,但是在sub不存在时会抛出异常。 12. isalnum(): 判断字符串是否仅由字母和数字组成。 13. isalpha(): 判断字符串是否仅由字母组成。 14. isdecimal(): 判断字符串是否仅由十进制数字组成。 15. isdigit(): 判断字符串是否仅由数字组成。 16. isidentifier(): 判断字符串是否为合法的标识符。 17. islower(): 判断字符串中所有字母是否都为小写。 18. isnumeric(): 判断字符串是否仅由数字字符组成。 19. isprintable(): 判断字符串是否可以打印(即不包含控制字符)。 20. isspace(): 判断字符串是否仅由空白字符组成。 21. istitle(): 判断字符串是否符合标题格式(即每个单词首字母大写)。 22. isupper(): 判断字符串中所有字母是否都为大写。 23. join(iterable): 将可迭代对象中的元素连接为一个字符串,元素之间用指定的字符串分隔。 24. ljust(width[, fillchar]): 将字符串左对齐,并在右侧填充指定的字符(默认为空格)。 25. lower(): 将字符串中所有字母转换为小写。 26. lstrip([chars]): 去掉字符串左侧指定的字符(默认为空格)。 27. maketrans(x[, y[, z]]): 创建一个字符映射表,用于translate()方法中的字符串替换。 28. partition(sep): 将字符串按照指定的分隔符(sep)分为三部分,返回一个元组。 29. replace(old, new[, count]): 将字符串中的旧字符串替换为新字符串。 30. rfind(sub[, start[, end]]): 在字符串中查找子字符串sub,并返回其最后一次出现的位置。可指定起始和结束位置。 31. rindex(sub[, start[, end]]): 在字符串中查找子字符串sub,并返回其最后一次出现的位置。与rfind()方法类似,但是在sub不存在时会抛出异常。 32. rjust(width[, fillchar]): 将字符串右对齐,并在左侧填充指定的字符(默认为空格)。 33. rpartition(sep): 将字符串按照指定的分隔符(sep)分为三部分,返回一个元组。 34. rsplit([sep[, maxsplit]]): 将字符串从右侧开始按照指定的分隔符(sep)进行分割,返回一个列表。 35. rstrip([chars]): 去掉字符串右侧指定的字符(默认为空格)。 36. split([sep[, maxsplit]]): 将字符串按照指定的分隔符(sep)进行分割,返回一个列表。 37. splitlines([keepends]): 将字符串按照换行符(\n)进行分割,返回一个列表。 38. startswith(prefix[, start[, end]]): 判断字符串是否以指定的前缀开头。可指定起始和结束位置。 39. strip([chars]): 去掉字符串两侧指定的字符(默认为空格)。 40. swapcase(): 将字符串中所有字母的大小写进行反转。 41. title(): 将字符串转换为标题格式(即每个单词首字母大写)。 42. translate(table[, deletechars]): 根据指定的字符映射表(table)替换字符串中的字符。 43. upper(): 将字符串中所有字母转换为大写。 44. zfill(width): 在字符串左侧填充0,使其达到指定的宽度。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值