Hackernoon logoBeginner Python Projects: How to Build an Acronym Generator by@mindninjax

Beginner Python Projects: How to Build an Acronym Generator

Author profile picture

@mindninjaxRishabh Singh

LIVE + LOVE + CODE!

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

How Do 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 the input()Ā method.

user_input = input("Enter a phrase: ")

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()

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 = ""

Now letā€™s create a for loop which will iterate through theĀ phraseĀ variable.

for i in phrase:
    acronym = acronym + word[0].upper()

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 on 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}')

Awesome now letā€™s try running our code with different inputs.

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

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

Tags

Join Hacker Noon

Create your free account to unlock your custom reading experience.