About bloggers : Former Internet manufacturer tencent staff , Network security giant Venustech staff , Alibaba cloud development community expert blogger , WeChat official account java Quality creators of basic notes ,csdn High quality creative bloggers , Entrepreneur , Knowledge sharers , Welcome to your attention , give the thumbs-up , Collection .
Python Is an easy to learn 、 Powerful programming language . It provides an efficient high-level data structure , It's also a simple and effective way of object-oriented programming .Python Elegant grammar and dynamic typing and the essence of interpretive language , Make it an ideal language for scripting and rapid application development on most platforms . Now let's introduce python Through string related knowledge to achieve a simple registration verification program .
example : Write a registration verification program , Set the following conditions : (1) The user name must be underlined “_” start , The length must be in 3~30 Between characters ; (2) Password must be underlined 、 Numbers and letters make up , No other symbols are allowed , The length must be in 8~16 Between characters .
The code is as follows , Each line is explained in the notes .
user_name = input(" Please enter a user name ( With “_” start ,3-30 Characters ):")
password = input(" Please input a password ( By underline 、 Numbers and letters make up ,8-16 Characters ):")
if user_name[0] != '_': # If user_name The first character of is not “_”
print(" Please start the user name with an underscore ") # Output “ Please start the user name with an underscore ”
elif 3 > len(user_name) or 30 < len(user_name): # If user_name The length is less than 3 Or greater than 30
print(" User name length exceeds the limit ") # Output “ User name length exceeds the limit ”
elif 8 > len(password) or 16 < len(password): # If password The length is less than 8 Or greater than 16
print(" Password length exceeds the limit ") # Output “ Password length exceeds the limit ”
elif password.find('_') == -1: # If password Does not exist in the “_”
print(" No underscore was entered in the password ") # Output “ No underscore was entered in the password ”
else: # None of the above conditions are met
psswords = password.replace('_', '1') # take password Replace the underline in with 1
if psswords.isalnum(): # passwords Whether there are only numbers or letters in
print(" Congratulations , Registered successfully ! user name :", user_name, ", password :", password)
else: # passwords There are characters other than numbers or letters in
print(" There are other symbols in the password , Registration failed !") # Output “ There are other symbols in the password …”
adopt pycharm The results are as follows , You can copy the code to understand .
1、 Liao Xuefeng's official website 2、python Official website 3、Python Programming case tutorial
That's about Python Through string related knowledge to achieve a simple registration verification program ., You can refer to it , Relevant knowledge will be continuously updated later , Make progress together .