Editor

Validate a Username

Beginner · Write the code · from Every Python String Method Explained with Examples

From the lesson: Every Python String Method Explained with Examples

Strings in Python come with a toolbox of built-in methods. These methods let you change case, remove extra spaces, search for words, split text apart, and much more.

Read the lesson

Write a program that validates a username based on these rules:

  • Must be alphanumeric (letters and numbers only)
  • Must be between 3 and 15 characters long
  • Three usernames are provided. For each one, print "Valid" if it passes both rules, or "Invalid" if it fails either rule.

    Expected output:

    player42: Valid
    ab: Invalid
    hello world!: Invalid
    usernames = ["player42", "ab", "hello world!"]
    # Check each username and print if it's valid or invalid

    More string methods exercises

    All Python exercises · 50 practice problems · Python Fundamentals quiz