Python Forum

Full Version: Regular Expressions with inputs
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi, I am trying to let a user input a username that is 8 characters long . Then if they enter it incorrectly, display an error message and ask them to enter a correct username. Every time I enter an 8 digit long username, it still says it is an invalid username. I have left the code below. Any help is appreciated.


import re
username = str(input("Please enter an 8 digit username: ")).lower()
if username ==("[a-z]{8}"):
    print("Valid username")
else:
    str(input("Please enter a valid username"))
You haven't actually used the re module here.
If you wanted to use re you would need to change your conditional to something like:
if re.match("^[a-z]{8}$", username):
but... don't do that.

Just check the length:
if len(username) == 8 and username.isalpha():

No need for regular expressions here at all.