python:标识符必须以字母或下划线开头,后面跟字母,下划线或者数字
2017-03-21 18:42
99 查看
标识符合法性检查,首先要以字母或者下划线开始,后面要跟字母,下划线或者或数字.这个小例子只检查长度大于等于 2 的标识符
运行结果 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
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 al 4000 phas + 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
相关文章推荐
- 判断字符串是否是这样组成的,第一个必须是字母,后面可以是字母、数字、下划线,总长度为5-20
- 正则表达式——判断字符串组成,第一个必须是字母,后面可以是字母、数字、下划线,总长度为5-20
- 输入框只能包括中文、英文字母、数字和下划线或者空格
- 自定义easyui整数或者数字、字母或者下划线验证方法
- js正则验证数字,字母和下划线(至少包含其中两种,数字不能开头)
- 正则表达式:检测数字字母和下划线组成的字符串,且字符串以字母开头
- 变量-变量名只能是字母、数字或者下划线的任意组合 变量名的第一个字符不能是数字
- Python判断字符串是否为字母或者数字(浮点数)的多种方法
- Android 密码校验,字符串中必须包含字母或者数字
- JS正则表达式验证账号、手机号、电话和邮箱 验证帐号是否合法 验证规则:字母、数字、下划线组成,字母开头,4-16位。 复制代码 function checkUser(str){ var
- Android中判断字符串中必须包含字母或者数字_Android
- JS 验证必须前面字母后面数字
- 正则表达式: 以英文字母开头,只能包含英文字母、数字、下划线
- JS 账号必须是数字或者字母
- 正则表达式:英文字母开头,只含有英文字母、数字和下划线
- Android 密码校验,字符串中必须包含字母或者数字
- Android中判断字符串中必须包含字母或者数字
- Android 设置登录密码必须是数字或者字母并弹出数字键盘
- 用户名要求 数字、字母、下划线的组合,其中数字和字母必须同时存在
- JS验证用户名必须以字母(不区分大小写)、数字、下划线(_)随意组合的字符