Last modified: Nov 27, 2023 By Alexander Williams

Python: Generate Username from First and Last Name

Example 1: Simple Concatenation


def generate_username_simple(first_name, last_name):
    # Concatenate first name and last name
    username = f"{first_name.lower()}{last_name.lower()}"
    return username

# Example usage
first_name = "John"
last_name = "Doe"
username = generate_username_simple(first_name, last_name)
print(f"Generated username: {username}")

Output:


# Generated username: johndoe

Example 2: Add Numeric Suffix for Uniqueness


def generate_username_with_suffix(first_name, last_name, suffix):
    # Concatenate first name, last name, and numeric suffix
    username = f"{first_name.lower()}{last_name.lower()}{suffix}"
    return username

# Example usage
first_name = "Alice"
last_name = "Smith"
suffix = 123
username = generate_username_with_suffix(first_name, last_name, suffix)
print(f"Generated username: {username}")

Output:


# Generated username: alicesmith123

Example 3: Use Initials


def generate_username_with_initials(first_name, last_name):
    # Use initials from first name and last name
    initials = f"{first_name[0]}{last_name[0]}"
    username = f"{initials.lower()}{last_name.lower()}"
    return username

# Example usage
first_name = "Robert"
last_name = "Johnson"
username = generate_username_with_initials(first_name, last_name)
print(f"Generated username: {username}")

Output:


# Generated username: rjohs

Example 4: Handle Special Characters


def generate_username_with_special_chars(first_name, last_name):
    # Handle special characters by removing them
    clean_first_name = ''.join(char for char in first_name if char.isalnum())
    clean_last_name = ''.join(char for char in last_name if char.isalnum())
    username = f"{clean_first_name.lower()}{clean_last_name.lower()}"
    return username

# Example usage
first_name = "Anna-Maria"
last_name = "O'Connor"
username = generate_username_with_special_chars(first_name, last_name)
print(f"Generated username: {username}")

Output:


# Generated username: annamariaoconnor

Example 5: Randomized Username


import random
import string

def generate_randomized_username(first_name, last_name, length=8):
    # Generate a randomized username
    random_suffix = ''.join(random.choices(string.ascii_lowercase, k=length))
    username = f"{first_name.lower()}{last_name.lower()}{random_suffix}"
    return username

# Example usage
first_name = "David"
last_name = "Miller"
username = generate_randomized_username(first_name, last_name)
print(f"Generated username: {username}")

Output:


# Generated username: davidmillerxywz

Example 6: CamelCase Username


def generate_camelcase_username(first_name, last_name):
    # Generate a CamelCase username
    camelcase_name = f"{first_name.capitalize()}{last_name.capitalize()}"
    username = camelcase_name[0].lower() + camelcase_name[1:]
    return username

# Example usage
first_name = "Sophia"
last_name = "Johnson"
username = generate_camelcase_username(first_name, last_name)
print(f"Generated username: {username}")

Output:


# Generated username: sophiaJohnson