Alexander Ramadan

Where I Sometimes Write Stuff

User Name Generator in Python

I am constantly finding myself needing to generate usernames for random accounts I signup for. I used to use the same username for everything, but I've tried to stop doing that since a lot of these accounts are just to try something or to get a freebie, so I don't want a giant paper trail of every account I've signed up for following around usernames I use for more important sites.

I used to use a few different websites to generate a user name, and those worked well, but they weren't quick enough for me. They usually forced me to choose from a number of categories like animals, cars or sports and it would create a unique name based off of my choices. That works well if I wanted a real looking user name, or something I planned on associating my online-self with, but most of the time I just want something quick that fits the necessary requirements set by the website for me to create a user name. These requirements usually include some or all of the following:

These seem like easy requirements, but when I'm asked to come up with a username I usually get frazzled and can't think of anything to put, so I decided to put together this little user name generator script to help me with this problem.

This python script does one thing, provides you with a random username with at least 5 characters (one capitalized) selecting from A-Z and 4 numbers (0-9). Now, when I need a quick user name I run this script from the terminal and copy and paste the user name into whatever profile I am filling out.

Here's how it works:

First you need to import the neccessary libraries

import string import random

Next we set the number of characters (A-Z) we want to include in our user name. We're setting this user name to have 5 characters.

First we're going to set the first letter of our user name name to be a capital. To do this first we need to set the number of characters we want to be capitalized. In my case I only want the 1 character to be a capital so I'll set it to you 1.

upper_case = 1

You can set this number to anything you want or name the variable you store it in whatever you want, but whatever number you set it to will generate a user name with that many capital letters.

Next we need to set a variable that will contain all the possible capital letters that our script will randomly choose from (A-Z).

name_upper_chars = string.ascii_uppercase

Then we need to tell python to randomly select one upper case character from the possible choices (A-Z). We're using the random.choice method to randomly select 1 character from the variable name_upper_chars which holds all our available characters. Essentially we're selecting 1 character randomly from A-Z and storing it in a new variable.

upper_name = ''.join(random.choice(name_upper_chars) for y in range(upper_case ))

Next, we're basically doing the same thing as before but for lowercase characters.

First we set the number of characters we want to get randomly generated

name_size = 4

Next we set a variable that will contain all the ascii lower case letters avaliable to use

name_chars = string.ascii_lowercase

Next we'll create the lowercase string part of our user name and store it in a variable to use later

name = ''.join(random.choice(name_chars) for x in range(name_size))

Next, we do the same thing as we did with the our uppercase and lowercase characters, but for the numbers that we want in our user name.

Set the number of numbers we want to be randomly generated and store that in a variable. Then we store all the possible choices we have (0-9) in another variable. Finally, use the random.choice method on the variable digits_chars to randomly select a number from our variable holding all our possible choices.

digits_size = 4 digits_chars = string.digits digits = ''.join(random.choice(digits_chars) for z in range(digits_size))
This will give us a string of 4 random numbers, from 0-9. Finally we combine our letters and numbers and print them to the screen
print uper_name + name + digits
It's pretty easy and simple. You can find it on Github if you wanted to grab it here Initially, when I first started working on this script I was trying to use separate functions that would create the random string of letters and numbers, combine and print them. The problem with this ended up being that the variables I was creating within each individual function yielded variables I couldn't use in other functions. I think global variables would have solved this, but from what I understand, you can't create global variables within a function, they need to be declared outside of any function to be used globally. I then thought a class with my functions nested within it would work. This way I could use my variables freely, but I couldn't get this method to work either (I'm still working on it though). Finally, I realized it didn't need to be this complicated and I could simply create my random strings individually, store them as variables and then combine and print them.