Content · Programming · Python

Python Create a Username from First and Last Names

A username can be derived from first and last names by normalizing case, trimming surrounding whitespace, and joining chosen parts. A production username policy also needs collision handling and validation. See two.

2 views Published Oct 17, 2026 Updated Oct 18, 2026 Admin

A classroom account system builds readable usernames for new learners.

What You Need to Understand


A username can be derived from first and last names by normalizing case, trimming surrounding whitespace, and joining chosen parts. A production username policy also needs collision handling and validation.

When to Use It


A classroom account system builds readable usernames for new learners.

Step-by-Step Python Example


first_name = "  Maya "
last_name = " Cohen  "
username = (first_name.strip()[:3] + last_name.strip()[:3]).lower()
print(username)

How the Example Works


The inputs are trimmed, the first three characters of each part are selected, and lowercase conversion produces maycoh.

Example Output


maycoh
kai.morgan

A Second Example


Compare this smaller variation with the main example, then experiment with your own values.

first_name = "Kai"
last_name = "Morgan"
username = f"{first_name.strip()}.{last_name.strip()}".casefold()
print(username)

Important Behavior and Edge Cases


Short names may contribute fewer than three characters. Never assume generated usernames are unique; avoid using names alone as a security credential.

Try It Yourself


Try short names such as Li and Wu; then devise a method for resolving duplicates.

Common Beginner Mistakes


  • Short names may contribute fewer than three characters. Never assume generated usernames are unique; avoid using names alone as a security credential.
  • Applying a string method before defining whether text is intended for display, comparison, parsing, or input validation.

Key Takeaways


  • A username can be derived from first and last names by normalizing case, trimming surrounding whitespace, and joining chosen parts.
  • The inputs are trimmed, the first three characters of each part are selected, and lowercase conversion produces maycoh.
  • Short names may contribute fewer than three characters.
Share this resourceSend this page to someone who may find it useful.