这期内容当中小编将会给大家带来有关Python如何实现判断字符串是否为合法标示符,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。
首先,我们来熟悉以下python标示符的定义是什么?
定义:以字母或下划线开始的,由字母,数字或下划线组成,但是不能是python的保留字。
又有疑问了,python有哪些保留字,分别是什么?
# python2.x import keyword print keyword.kwlis # python3.x import keyword print(keyword.kwlist) # python2.x输出: ['and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'exec', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'not', 'or', 'pass', 'print', 'raise', 'return', 'try', 'while', 'with', 'yield'] # 共31个 # python3.x输出: ['False', 'None', 'True', 'and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield'] # 共33个
好了,下面开始判断
# python2.7 #!/usr/bin/env python # -*- coding: UTF-8 -*- import keyword import string def is_signal(s): kw = keyword.kwlist if s in kw: return 0 elif s[0] == '_' or s[0] in string.letters: # 判断是否为字母或下划线开头 for i in s: if i == '_' or i in string.letters or i in string.digits: # 判断是否由字母数字或下划线组成 pass else: return 0 return 1 else: return 0 def main(): s = raw_input() if is_signal(s) == 1: print "True" else: print "False" if __name__ == '__main__': main() # python3.4 #!/usr/bin/env python # -*- coding: UTF-8 -*- import keyword import string def is_signal(s): kw = keyword.kwlist if s in kw: return 0 elif s[0] == '_' or s[0] in string.ascii_letters: # 判断是否为字母或下划线开头 for i in s: if i == '_' or i in string.ascii_letters or i in string.digits: # 判断是否由字母数字或下划线组成 pass else: return 0 return 1 else: return 0 def main(): s = input() if is_signal(s) == 1: print("True") else: print("False") if __name__ == '__main__': main()
通过键盘输入判断,是标示符,则返回True,否则返回False
补充知识:python:标识符必须以字母或下划线开头,后面跟字母,下划线或者数字
标识符合法性检查,首先要以字母或者下划线开始,后面要跟字母,下划线或者或数字.这个小例子只检查长度大于等于 2 的标识符
idcheck.py
#!/usr/bin/env python ''' idcheck.py -- checks identifiers for validity ''' import string # string utility module # create alphabet and number sets alphas = string.ascii_letters + '_' nums = string.digits # salutation message and input prompt print ('Welcome to the Identifier Checker v1.0') print ('Testees must be at least 2 chars long.') inp = input('Identifier to test ?') if len(inp) >= 1: if inp[0] not in alphas: print ('invalid: first symbol must be alphabetic') else: for otherChar in inp[1:]: if otherChar not in alphas + nums: print ('invalid: remaining symbols must be alphanumeric') break else: print ("okay as an identifier") else: print ('invalid: length must be >= 1')
运行结果 1:
Welcome to the Identifier Checker v1.0 Testees must be at least 2 chars long. Identifier to test -> 123_das invalid: first symbol must be alphabetic
运行结果 2:
Welcome to the Identifier Checker v1.0 Testees must be at least 2 chars long. Identifier to test -> _123sdad okay as an identifier
上述就是小编为大家分享的Python如何实现判断字符串是否为合法标示符了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。