DEV Community

Cover image for How to build an Acronym Generator in Python
Rishabh Singh ⚡
Rishabh Singh ⚡

Posted on • Updated on

How to build an Acronym Generator in Python

Hey everyone, today we are going to create a simple Acronym Generator using Python.

Alt Text

How Acronym Generators work?

So basically an Acronym Generator will take a String as an input and it will return the initials of all the words in the String.

Let's Code

To get started, we need a phrase from the user. We can do that using input() method.

user_input = input("Enter a phrase: ")
Enter fullscreen mode Exit fullscreen mode

We have stored the user input in a user_input variable.

Now we must ignore words like 'of' from the user input as most of the time, 'of' is not considered for acronyms.

Also, we need to separate each word and store it individually in a form of a list so that we can easily iterate through it.

phrase = (user_input.replace('of', '')).split()
Enter fullscreen mode Exit fullscreen mode

Here in user_input.replace('of', '') we are using .replace() function to ignore 'of' from the input, if any.

And then we are using .split() function to break down the string into individual words and store them as a list in phrase variable.

We are almost done!

We need an empty string variable to store our acronym. Let's quickly create one...

acronym = ""
Enter fullscreen mode Exit fullscreen mode

Now let's create a for loop which will iterate through phrase variable.

for word in phrase:
    acronym = acronym + word[0].upper()
Enter fullscreen mode Exit fullscreen mode

Here in acronym = acronym + word[0], we are slicing off the first letter of words stored in phrase using slicing operator and adding it to our acronym variable.

We are also using .upper() function to capitalize the acronyms.

Finally, just add a print statement which will print out the acronym on the console.

print(f'Acronym of {user_input} is {a}')
Enter fullscreen mode Exit fullscreen mode

Awesome now let's try running our code with different inputs.

Enter a phrase: machine learning
Acronym of machine learning is ML
Enter fullscreen mode Exit fullscreen mode
Enter a phrase: artificial intelligence
Acronym of artificial intelligence is AI
Enter fullscreen mode Exit fullscreen mode
Enter a phrase: federal bureau of investigation
Acronym of federal bureau of investigation is FBI
Enter fullscreen mode Exit fullscreen mode

Source Code

You can find the complete source code of this project here - mindninjaX/Python-Projects-for-Beginners

Support

Thank you so much for reading! I hope you found this beginner project useful.

If you like my work please consider Buying me a Coffee so that I can bring more projects, more articles for you.

Also if you have any questions or doubts feel free to contact me on Twitter, LinkedIn & GitHub. Or you can also post a comment/discussion & I will try my best to help you :D

Top comments (6)

Collapse
 
surajondev profile image
Suraj Vishwakarma

Great tutorial with getting started to operate on a string which is very important for beginners. Things to learn from this tutorial is

  • split function
  • iterating through the string
  • changing case of a letter

Great Work

Collapse
 
mindninjax profile image
Rishabh Singh ⚡

I am glad you liked it Suraj!
Thanks for the takeaway summary 😇

Collapse
 
michaelcurrin profile image
Michael Currin

Can I suggest word as variable name in place of i?

Collapse
 
mindninjax profile image
Rishabh Singh ⚡

Thank you so much, Mike, for the suggestion... I have fixed the mistake...
Again thank you so much and have a great day 😃😄😊

Collapse
 
alex17pat profile image
alex

Thanks for the question. I encountered this acronym many times, but have never thought about it.
It stands for “Completely Automated Public Turing test to tell Computers and Human Apart”.
It looks like have already been absorbed into dictionary, so it can be used as a natural English word without capital.
Nowadays, “captcha” is used to distinguish human and computer by many websites to prevent automated cyber attack. wordmaker.info/how-many/acronym.html

Collapse
 
ayush2007a profile image
Ayush Moghe

you made a superb thing in very short code.Nice!