Username Generator • Beginner Python Program

Table of Contents

Usernames, we all have one, maybe for work or on Twitter.

How about a python program that automatically generates a username based on your name?

Sound cool? Let’s make a simple project to generate usernames.

See the live program below:

Disclaimer, this is not a python tutorial, for that, see Intro To Python.

1. Core Code

Let’s get the core elements down for the username generator.

First, displaying the title via print() and then using two variables with input(). Then at the end, print() the concatenated string to combine the variables.

We’ll just comment the slicing out for now and tackle that at the next stage.

print('Welcome to the Username Generator Program.')
first = input('Enter first name: ')
last = input('Enter last name: ')
# Slicing done here
print('Your username is: ' + last + first)

2. Slicing

Great, let’s slice out the sections of user input we need to create the username.

Now the username is based on the basic nomenclature of using the first four letters of the last name, and the first letter of the first name.

In order to program python to do this, we need to use slices.

We’ll assign the first and last variables with their sliced counterparts.

For the last variable, it starts at the first character, 0 up to but not including 4. Then, get the first letter from the first variable by stating 0.

That’s it!

print('Welcome to the Username Generator Program.')
first = input('Enter first name: ')
last = input('Enter last name: ')
last = last[0:4] # Slice first letter up to but not including 4 
first = first[0] # Slice first letter, starting at 0
print('Your username is: ' + last + first)

Thank you to w3schools.

3. String Conversion

The last step is to convert the concatenated output to lower case characters.

Generally, usernames are in lower case, plus this is good practice to use the str.lower() function.

print('Welcome to the Username Generator Program.')
first = str(input('Enter first name: '))
last = str(input('Enter last name: '))
last = last[0:4] # Slice first letter up to but not including 4 
first = first[0] # Slice first letter, starting at 0
print('Your username is: ' + str.lower(last) + str.lower(first)) # Change to lower case for username format

Thank you to educba.

4. Reflection

This was a fun small project I thought of when I woke up this morning.

It’s a popular username format so it’s fun thinking about how to automate its creation. I can imagine this built into a workplace app to spit out the new hire’s username.

It’s my first time using slices outside following walkthrough projects from Automate The Boring Stuff with Python, so I’m surprised I got it so quickly.

This program only took a few minutes to complete, a lot faster than I was expecting.

This is Day 24 of #100DaysOfHacking, subscribe to the newsletter for updates and if you have feedback, message me via Twitter. Happy Hacking.

About The Author
Ashley Ball

Ashley Ball

“Learn, create, share, repeat.” • IT teacher, former web designer, learning #cybersec • Road to #100DaysOfHacking
Share This Article
Share on linkedin
LinkedIn
Share on twitter
Twitter
Share on facebook
Facebook
Share on whatsapp
WhatsApp
Share on email
Email

Monthly Newsletter

Learn about design, web tech and cyber security.

Monthly Newsletter

Learn about design, web tech and cyber security.

This site uses cookies and other tracking technologies to assist with navigation, monitor site usage and web traffic, assist with our promotional and marketing efforts, and customize and improve our services, as set out in our privacy policy