String operator
Comparison operators: String sizes are compared by ASCLL code value size
Slice operator: [], [:], [::]
Member relationship operators: in, not in
Script: Determine if the user entered an ID that meets the requirements
#!/usr/bin/env python
Import string
#定义字符的范围
first_id = String.letters + "_"//determines the range of the first character
all_id = string.digits + first_id//Determining the range of other characters
#请求用户输入id
myID = raw_input ("Input your ID:")//request user input ID
#判断id是否合法
If Len (myID) < 2:
Print "You must input ID longer then 2."
Else
If myid[0] not in first_id:
Print "First char%s is invalid"% (Myid[0])
Else
For ch in myid[1:]:
If CH not in all_id:
Print "Other char invalid"
Break
Else
Print "%s is valid"% (myID)
The following is a function that can be called
#!/usr/bin/env python
Import string
FIRST_CHR = String.letters + "_"
ALL_CHR = FIRST_CHR + string.digits
myID = raw_input ("Input your ID:")
def id_check (myID):
If Len (myID) < 2:
Print "You must input char longer than 2."
Else
If myid[0] not in FIRST_CHR:
Print "Your first char is invalid"
Else
For I in range (Len (myid[1:])):
IND = i + 1
If Myid[ind] not in ALL_CHR:
Print "char is invalid in position%s"% IND
Else
Print "Your ID is valid"
if __name__ = = "__main__":
Id_check ("123")
Id_check ("abc")
This article is from the "Court of the Odd Tree" blog, please be sure to keep this source http://zhangdl.blog.51cto.com/11050780/1829113
To write: A string of Python data types to determine if the user entered an ID that meets the requirements