Make a Username
Topic: Username Generator & Validator
Description: This interactive program takes a user's first name, last name, and birth year to automatically generate a clean, standardized corporate username. It then validates the username against strict security policy rules (checking for length constraints, spaces, and banned alphanumeric patterns).
Why: This program introduces students to fundamental **string manipulation techniques** (such as concatenation, casing modifiers, and string slicing). It also teaches how to combine data validation logic with string operations to sanitize raw inputs into structured configuration handles.
Program Code
Copy and run the following Python code to see the username processing engine in action:
def process_username_system(): print("=============================") print(" USERNAME GENERATION ENGINE ") print("=============================") # Step 1: Collect User Details first_name = input("Enter your first name: ").strip() last_name = input("Enter your last name: ").strip() birth_year = input("Enter your birth year (YYYY): ").strip() # Step 2: Validate Raw Inputs if not (first_name.isalpha() and last_name.isalpha()): print("\nError: Names must only contain alphabetical characters.") return if not (birth_year.isdigit() and len(birth_year) == 4): print("\nError: Birth year must be a 4-digit number.") return # Step 3: Generate System Username using Slicing # Pattern: first 3 letters of first name + first 3 letters of last name + last 2 digits of year username = first_name[:3].lower() + last_name[:3].lower() + birth_year[-2:] print("\n-----------------------------") print(" GENERATED CREDENTIAL ") print("-----------------------------") print(f"Suggested Username : {username}") print("-----------------------------") # Step 4: Security Policy Validation Checks print("\nRunning Security Diagnostics...") is_valid = True reasons = [] # Rule A: Minimum Length check if len(username) < 6: is_valid = False reasons.append("Username string length is too short (Must be >= 6 characters).") # Rule B: Specific banned sequence block check if "admin" in username or "root" in username: is_valid = False reasons.append("Contains reserved system words ('admin' or 'root').") # Print Compliance Report Card if is_valid: print("Status : APPROVED") print("Message : Username meets corporate directory policies.") else: print("Status : REJECTED") for reason in reasons: print(f" - Violation : {reason}") # Execute the engine profile process_username_system()
Expected Console Output Examples
Example 1 (Standard Compliant Profile):
=============================
USERNAME GENERATION ENGINE
=============================
Enter your first name: John
Enter your last name: Miller
Enter your birth year (YYYY): 1994
-----------------------------
GENERATED CREDENTIAL
-----------------------------
Suggested Username : johmil94
-----------------------------
Running Security Diagnostics...
Status : APPROVED
Message : Username meets corporate directory policies.
Example 2 (Short Input Exception Profile):
Enter your first name: Ed
Enter your last name: Li
Enter your birth year (YYYY): 2005
-----------------------------
GENERATED CREDENTIAL
-----------------------------
Suggested Username : edli05
-----------------------------
Running Security Diagnostics...
Status : REJECTED
- Violation : Username string length is too short (Must be >= 6 characters).
Core Syntax & Concept Breakdown
- String Slicing (
[start:stop]): Python allows you to extract segments of a string.first_name[:3]grabs characters from index 0 up to (but excluding) 3. Negative indexing likebirth_year[-2:]isolates the final 2 characters of the string. - Input Sanitization (
.strip()&.lower()): The.strip()function removes accidental trailing or leading spaces entered by users, while.lower()converts the characters to lowercase to preserve system consistency. - Character Condition Inferences (
.isalpha()&.isdigit()): Built-in string methods allow data typing verification.isalpha()confirms that a string contains only letters, whileisdigit()guarantees a string contains only numeric characters.
🏋️ Test Yourself With Exercises
Take our quiz on Make a Username to test your knowledge.
Browse Quizzes »