Auto-Suggest Usernames From Seed Keywords in Python

Featured Img Username Constraints

In this tutorial, we learn how to suggest usernames by adding constraints in Python. We have set the following constraints before generating a username:

  1. Atleast two Capital Letters
  2. In special characters only . , - and _ allowed
  3. Atleast 3 digits present

Auto-Suggest Usernames in Python

To make it more meaningful to the user we will first take input data from the user and on the basis of the input data, we will be suggesting a username to them. Let’s look at the complete code step by step.

Step 1: Starting of a username

Now the usernames mostly start from either ‘#’ or ‘@’. We will keep the starting of our usernames with a hashtag ( # ). You can keep whatever symbol you want.

Step 2: Taking User Information

Obviously, we want the username to have some meaning for the user and the user must relate to it somehow to make it easier for them to remember.

The simplest information one can get about a person is its name and here we will be considering the full name of the user.

Step 3: Adding the Constraints

Next step, we’ll be adding the following constraints to our username generation code.

1. Atleast two Capital/Uppercase Letters

We’ll use the initials from the first and last names to fulfill this constraint.

2. Special Character Addition

Only 3 special characters are allowed which are ‘.’, ‘-‘ and ‘_’.

So after the uppercase letters, we will insert a special character. You can place the characters at any position you wish to just change the order of the statements.

3. Atleast three digits and some random lowercase letters

The last constraint is to have a combination of lowercase letters and at least three digits.

The number of lowercase letters depends on the length of the username and in our case, we will keep the length of the username as 10.

In these 10 characters by now, four characters are already filled by ‘#’, two uppercase characters, and a special character.

For the lowercase letters to make it simpler, we will be choosing random characters from the leftover letters from the name of the user. And we will be selecting three random digits from 0 to 9.

We will keep the final order of the username as shown below.

# + 2 Uppercase characters + . or - or _ + 3 Lowercase characters + 3 Digits

Auto-Suggest Usernames using Python [Implementation]

The complete code implementation is shown below and comments are added for your understanding.

# Taking input of name of the user
name  = input("Enter your full name: ")

# Initializing the username
username = "#"

# 1. First two uppercase letter
l = name.split()
# Name must have both first and last name
    name = input("Enter full name please: ")
    l = name.split()
username += l[0][0].upper()

# 2. Adding special character ( . , _ or -)
import random
choice = random.choices(".-_", k=1)
username += choice[0]

# 3. Atleast three digits : The 3 digits chosen ( will be added after lowecase letters)
digits_chosen = random.choices("0123456789",k=3) 

# 4. Lowercase letters ( 3 )
remaining = l[0][1:] + l[1][1:]
letters_chosen = random.choices(remaining,k=3)

# 5. Include the three lower and then three digits
username = username+  letters_chosen[0] + letters_chosen[1] + letters_chosen[2]
username = username + digits_chosen[0] + digits_chosen[1] + digits_chosen[2]

print("The Final Username Generated is: ", username)


The code was tested for some random inputs. You can have a look at them yourself.

Enter your full name: Isha Bansal
The Final Username Generated is:  #IB-sha914

And in case the user doesn’t enter its full name then the program will ask to enter again.

Enter your full name: Kartik
Enter full name please: Kartik Gupta
The Final Username Generated is:  #KG_iat397

I hope you understood the logic and the implementation of the problem. You can set and change constraints according to your own preference.

Thank you for reading!