Python字符串isidentifier()
Python String isidentifier() function returns True
if the string is a valid identifier according to the Python language definition.
![](/assets/blank.gif)
如果字符串是根据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 unicodedatacount = 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.
请注意,由于有效的标识符字符数量巨大,因此我在输出中仅提供了几个字符。
Reference: Official Documentation, PEP-3131
参考: 官方文档 , PEP-3131
翻译自: https://www.journaldev.com/24059/python-string-isidentifier
Python字符串isidentifier()相关推荐
- Python字符串基本操作
Python字符串基本操作 1.判断是不是合法的标识符isidentifier name="ABC" print(name.isidentifier()) 打印结果 True 2. ...
- 2.1.Python字符串处理(去掉空格或者特殊字符、替换操作、查找操作、判断操作、分割合并操作、字符串文档)
2.1.Python字符串处理 2.1.1.去掉空格或者特殊字符 2.1.2.替换操作 2.1.3.查找操作 2.1.4.判断操作 2.1.5.分割合并操作 2.1.6.字符串文档 2.1.Pytho ...
- python字符串函数运算_Python入门教程2. 字符串基本操作【运算、格式化输出、常用函数】 原创...
前面简单介绍了python基本运算,这里再来简单讲述一下Python字符串相关操作 1. 字符串表示方法 >>> "www.jb51.net" #字符串使用单引号 ...
- python字符串的表示_Python字符串方法总结
Python字符串方法图示: (温馨提示:对图片点右键--在新标签页中打开图片) 1.index() 定义:查找并返回指定str的索引位置,如果没找到则会抛异常(查找的顺序是从左至右)可以指定范围:开 ...
- python print 输出到txt_(Python基础教程之七)Python字符串操作
Python基础教程 在SublimeEditor中配置Python环境 Python代码中添加注释 Python中的变量的使用 Python中的数据类型 Python中的关键字 Python字符串操 ...
- 以30字符宽居中输出python字符串_python3字符串
字符串 字符串是 python 中最常用的数据类型.我们可以使用引号( ' 或 " )来创建字符串.创建字符串很简单,只要为变量分配一个值即可.例如: name = 'wingis' pyt ...
- python字符串(连载二)|字符串内置函数原来还可以这么玩!
此系列连载涵盖 字符串,列表,字典,元组,集合等内置函数的基本用法.每次更新十个函数的基本用法.(建议收藏哈) 有福利哦,小编整理了些python学习资料,需要的自行领取!获取方式在文末哈! 目录: ...
- python字符串_(Python基础教程之七)Python字符串操作
Python基础教程 在SublimeEditor中配置Python环境 Python代码中添加注释 Python中的变量的使用 Python中的数据类型 Python中的关键字 Python字符串操 ...
- 来不及解释!python字符串常用方法大全,先收藏再说
目录 序言: 1.0 capitalize()函数 2.0 title()函数 3.0 swapcase()函数 4.0 lower()函数 5.0 upper()函数 6.0 casefold()函 ...
最新文章
- [ Linux ] 釋放記憶體指令(cache) - 轉載
- Unity3D学习笔记(一) 模型和贴图导入学习
- Gradle 编译速度提升 70%!怎么做到的?
- 礼赞 Wordpress,蝉知可直接使用 Wordpress 模板
- Codeup墓地-问题 D: 继续畅通工程
- 知识蒸馏在广告系统中的应用(一)
- 4个不错的Flash开源项目
- 最简单的视音频播放示例2:GDI播放YUV, RGB
- MediaCodec进行编解码AAC(文件格式转换)
- Quartz 视频教程免费下载
- Python实现熵权法(正负指标)并计算综合评分—————附增完整代码和测试用例
- 计算机的硬盘和光驱的接口是什么类型的接口,连接硬盘和光驱是什么接口
- AndroidStudio报错解决方案
- Team Foundation Server BuilderService LOG
- 第三阶段应用层——1.4 数码相册—FreeType介绍
- Cg Programming In Unity Specular Highlights (Wiki翻译自用)
- 计算机考研失败后,大部分人的找工作现状
- FFB6D A Full Flow Bidirectional Fusion Network for 6D Pose EstimationFFB6D 6D 姿势估计的全流双向融合网络
- 中国主要湖泊水库面状矢量边界图层ArcGIS使用之一篇
- delete 和 delete []的真正区别
热门文章
- 关于升级到win10后的网络问题
- Server SAN:弄潮儿云计算时代
- WPF DataGrid 样式分享
- SQL2005安装及链接
- [转载] Python 3 集合方法 remove( )
- 39. 组合总和 ,40. 组合总和 II,216. 组合总和 III
- python进程问题
- cobbler的搭建
- [BZOJ1602BZOJ1787BZOJ2144]树上LCA的算法巩固练习
- ZOJ 1315【Excuses, Excuses!】------2015年2月9日