To write: A string of Python data types to determine if the user entered an ID that meets the requirements

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.