acronym is a shortened version of a long phrase, for instance, we have NLP for natural language processing. In this tutorial, I’ll show you how to create a Python application that converts text to acronyms.
While spelling out the whole word takes longer, saying or writing the first initial of each word or an abbreviated form of the full word takes less time. As a result, employing acronyms and abbreviations in ordinary speech makes communication easier.
Also read: Crypto Price Prediction with Python
Converting Text to Acronyms in Python
To implement acronyms, our aim is to generate a short form of a word from a given sentence. The same can be done by splitting and indexing to get the first word and then combine it.
Look at the code mentioned below and then we will go inside the code line by line.
Phrase = input("Enter a Phrase to convert: ") list_words = Phrase.split() final_acro = "" for i in list_words: final_acro+=i.upper() print("Final Acroynm : ",final_acro) for i in range(len(final_acro)): print(final_acro[i]," -- ",list_words[i])
Line 1takes the input of the Phrase which needs to be converted into an acronym with the help of the input function.
Line 2will convert the sentence into a list of words with the help of the split function which will by default split the sentence on the basis of whitespaces.
Line 3is initializing the final acronym with an empty string which will be changed later on.
Line 5, we have a loop that will add the first letter of each word as a character in the final acronym.
- The acronyms always include uppercase letters hence, each first letter in every word is converted to uppercase regardless of their previous case with the help of the upper function.
Line 9will display the final acronym that has been created with the help of loops and print statements.
The images below display some sample outputs after executing the code mentioned in the previous section.
I hope you understood acronyms and how to implement the same in python. Try it yourself!
Happy Coding! 😇
Want to learn more? Check out the tutorials mentioned below:
- Find Number of Possible Strings With No Consecutive 1s
- How to convert a dictionary to a string in Python?
- Convert a Tuple to a String in Python [Step-by-Step]
- String Formatting in Python – A Quick Overview