Saturday, December 28, 2024
Google search engine
HomeLanguagesHow to generate a unique username using Python

How to generate a unique username using Python

A username is a unique name that is mainly used on websites and apps. A username is used to identify a person. For example, if your name is Akshay and you want to create your account on any social media app, here we need a username such that another person can find us. It is not possible to search by Akshay because there may be many users with the name Akshay.

In this article, we will learn to generate username suggestions. We will use some constraints to generate a username. 

Approach: 

We will first take the name of the user, remove space from it and convert it to lowercase. Then we will take some part of the name of the user and then add some digits and special symbols to generate our username.

Steps to generate username:

  • Import Modules and take the name of the user.

Python3




import random
 
name_of_user = "Akshay Singh"


  • Define Constraints like the minimum length of the username, the minimum number of special characters, the minimum number of digits, etc.

Python3




# Constraints
minimum_capital_letter = 1
minimum_specia_char = 1
minimum_digits = 2
min_len_of_username = 8
special_chars = ['@','#','$','&']


  • Define a variable to store the username, remove space from the name of the user and convert it to lowercase.

Python3




# variable to store generated username
username = ""
 
# remove space from name of user
name_of_user = "".join(name_of_user.split())
 
# convert whole name in lowercase
name_of_user = name_of_user.lower()


  • Define the minimum number of characters that we need to take from the name of the user and then take a random number of characters from the name of the user using random.randint(start,end) function. 
random.randint(start,end) function returns a random integer between start and end.
start and end are also included in this range.
Example: random.randint(3,8) will return a number between 3 and 8, including 3 & 8.

Python3




# calculate minimum characters that we need to take from name of user
minimum_char_from_name = min_len_of_username-minimum_digits-minimum_specia_char
 
# take required part from name
temp = 0
for i in range(random.randint(minimum_char_from_name,len(name_of_user))):
    if temp < minimum_capital_letter:
        username += name_of_user[i].upper()
        temp += 1
    else:
        username += name_of_user[i]


  • The last step is to take random digits and special characters in a list and then shuffle them using random.shuffle() and then insert them to last in username.

Python3




# temp_list to store digits and special_chars so that they can be shuffled before adding to username
temp_list = []
# add required digits
for i in range(minimum_digits):
    temp_list.append(str(random.randint(0,9)))
 
# append special characters
for i in range(minimum_specia_char):
    temp_list.append(special_chars[random.randint(0,len(special_chars)-1)])
 
# shuffle list
random.shuffle(temp_list)
 
username += "".join(temp_list)
 
print(username)


Code:

Example 1: 

To generate 5 usernames of minimum length 8, the minimum number of digits 2, the minimum number of special characters 2, and minimum capital letters 2

Python3




import random
 
def generate_username(name_of_user):
    # Constraints
    minimum_capital_letter = 2
    minimum_specia_char = 2
    minimum_digits = 2
    min_len_of_username = 8
    special_chars = ['@','#','$','&']
 
    # variable to store generated username
    username = ""
 
    # remove space from name of user
    name_of_user = "".join(name_of_user.split())
 
    # convert whole name in lowercase
    name_of_user = name_of_user.lower()
 
    # calculate minimum characters that we need to take from name of user
    minimum_char_from_name = min_len_of_username-minimum_digits-minimum_specia_char
 
    # take required part from name
    temp = 0
    for i in range(random.randint(minimum_char_from_name,len(name_of_user))):
        if temp < minimum_capital_letter:
            username += name_of_user[i].upper()
            temp += 1
        else:
            username += name_of_user[i]
 
    # temp_list to store digits and special_chars so that they can be shuffled before adding to username
    temp_list = []
    # add required digits
    for i in range(minimum_digits):
        temp_list.append(str(random.randint(0,9)))
 
    # append special characters
    for i in range(minimum_specia_char):
        temp_list.append(special_chars[random.randint(0,len(special_chars)-1)])
 
    # shuffle list
    random.shuffle(temp_list)
 
    username += "".join(temp_list)
 
    print(username)
 
 
if __name__ == "__main__":
    name_of_user = "Akshay Singh"
    for i in range(5):
        generate_username(name_of_user)


Output:

 

Example 1: 

To generate 8 usernames of minimum length 6, the minimum number of digits 1, the minimum number of special characters 2, and minimum capital letters 1

Python3




import random
 
def generate_username(name_of_user):
    # Constraints
    minimum_capital_letter = 1
    minimum_specia_char = 2
    minimum_digits = 1
    min_len_of_username = 6
    special_chars = ['@','#','$','&']
 
    # variable to store generated username
    username = ""
 
    # remove space from name of user
    name_of_user = "".join(name_of_user.split())
 
    # convert whole name in lowercase
    name_of_user = name_of_user.lower()
 
    # calculate minimum characters that we need to take from name of user
    minimum_char_from_name = min_len_of_username-minimum_digits-minimum_specia_char
 
    # take required part from name
    temp = 0
    for i in range(random.randint(minimum_char_from_name,len(name_of_user))):
        if temp < minimum_capital_letter:
            username += name_of_user[i].upper()
            temp += 1
        else:
            username += name_of_user[i]
 
    # temp_list to store digits and special_chars so that they can be shuffled before adding to username
    temp_list = []
    # add required digits
    for i in range(minimum_digits):
        temp_list.append(str(random.randint(0,9)))
 
    # append special characters
    for i in range(minimum_specia_char):
        temp_list.append(special_chars[random.randint(0,len(special_chars)-1)])
 
    # shuffle list
    random.shuffle(temp_list)
 
    username += "".join(temp_list)
 
    print(username)
 
 
if __name__ == "__main__":
    name_of_user = "Akshay Singh"
    for i in range(8):
        generate_username(name_of_user)


Output:

 

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments