An acronym generator generates a word from a phrase using keyword abbreviations. Individual letters from the words contained in a phrase are used to create a new word, which we refer to as acronym.
How an acronym generator works?
An Acronym Generator will take a String as an input and it will return the initials of all the words in the String.
How to build an acronym generator?
To begin with , we need a phrase or word from the user. We can do that using input() method.
user_input = input("Enter a phrase: ")
We have stored the user input in a user_input variable.
Now that we have stored our user input, we must ignore words like 'of' , 'and' from the user input as most of the time, these words are 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.
In the user_input.replace() , we are using .replace() function to ignore 'of' and 'and' from the input, if any.
Then we are using .split() function to break down the string into individual words and store them as a list in phrase variable.
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 help iterate through the phrase variable
for word in phrase: acronym = acronym + word.upper()
In acronym = acronym + word, 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.
Lastly, just add a print statement which will print out the acronym as our output.
print("The acronym of " + " " + user_input + " " + "is" + " :" + " " + acronym)