How to generate a unique username using Python
Last Updated :
28 Apr, 2025
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:
Similar Reads
How to generate a random phone number using Python? In this article, we will learn how to generate a random phone number using Python. In general, Indian phone numbers are of 10 digits and start with 9, 8, 7, or 6. Approach: We will use the random library to generate random numbers.The number should contain 10 digits.The first digit should start with
1 min read
Python PRAW â Getting the username of a redditor In Reddit, a redditor is the term given to a user. Each and every redditor has a username chosen by them during registration. Here we will see how to fetch the username of a redditor. We will be using the name attribute of the Redditor class to fetch the username. Example 1 : Consider the following
2 min read
How to get the current username in Python When building interacting Python programs you might need to get the current username for features like personalizing user experience, providing user-specific resources, Implementing robust logging and debugging, strengthening security, etc. We will use different Python modules and functions like os.
4 min read
GUI to generate and store passwords in SQLite using Python In this century there are many social media accounts, websites, or any online account that needs a secure password. Â Often we use the same password for multiple accounts and the basic drawback to that is if somebody gets to know about your password then he/she has the access to all your accounts. It
8 min read
How to Generate a Random Password Using Ruby? Generating a random password is a fundamental task in cybersecurity and application development. Creating a secure random password is very easy with the Ruby library. This article will show how to generate a random password in Ruby in Python. Generate a Random Password Using RubyBelow are the code e
2 min read
Python | Random Password Generator using Tkinter With growing technology, everything has relied on data, and securing this data is the main concern. Passwords are meant to keep the data safe that we upload on the Internet. An easy password can be hacked easily and all personal information can be misused. In order to prevent such things and keep th
4 min read
Generating Strong Password using Python Having a weak password is not good for a system that demands high confidentiality and security of user credentials. It turns out that people find it difficult to make up a strong password that is strong enough to prevent unauthorized users from memorizing it. This article uses a mixture of numbers,
3 min read
Create a Random Password Generator using Python In this article, we will see how to create a random password generator using Python. Passwords are a means by which a user proves that they are authorized to use a device. It is important that passwords must be long and complex. It should contain at least more than ten characters with a combination
5 min read
Python Tweepy - Getting the name of a user In this article we will see how we can get the name of a user. Name is the display name of the twitter account. It is the name that users choose to identify themselves on the network. Many users choose to either use their real name as the basis for their display name. Unlike screen names, the names
2 min read
Deleting a User in Linux using Python Script Deleting a user from your system or server via a python script is a very easy task. You just need to pass the username of the user and the script will remove the details and all the files of that user.This python script uses userdel Linux command to delete the user.You can directly use userdel comma
2 min read