Username Suggestions based on Constraints | Python
In this article, we will see how to generate Username Suggestions based on certain constraints in Python language. Usernames are very common on the web nowadays. They are the short-names by which people remember you. So, if you want create a username, it should follow a pattern leading to something meaningful.
This article just tries to give us an idea of how we can approach the problem of providing Username Suggestions for a person’s username.
Username Suggestions from Name of the User
Consider you have constraints such as:
- There should be at least one Capital letter.
- All other letters should be lowercase.
- The special characters allowed are only
- The username should have at least two digits.
This problem can be followed by the code mentioned below:
import random import string # Initializing the username with @ username = '@' # Taking the Full Name as input, converting it to lowercase and splitting it. name = input().lower().split() # Taking the first letter of first name in uppercase as the first letter in the username. username += name.upper() # Choosing between . and _ username += ''.join(random.choices('._', k=1)) # Including the first half of the last name username += name[:int(len(name) / 2)]' # Choosing 2 digits from 0 to 9 username += ''.join(random.choices(string.digits, k=2)) # Printing the username generated print(username)
Karan Trehan Devansh Duggal
This is one way how we can approach this problem by creating username out of the name of the person. Subsequently, this code can be scaled by implementing a database connection. So, everytime, whenever a new username is to be created, the username is firstly checked inside the Database to prevent duplicality.
The other scenario that could be integrated would be creating username based on the form filled by the user. After that, picking up the details and merging them to form a useful and meaningful username.
The major part is how do we take care of the constraints. So, to tackle that, develop algorithms like the one above and keep testing them.
Hope, you enjoyed this tutorial and it helped you to solve your problem. You can also checkout: