Convert Text to Acronyms in Python – A Complete Guide

Feautured Img

An 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[0].upper()

print("Final Acroynm : ",final_acro)
for i in range(len(final_acro)):
    print(final_acro[i]," -- ",list_words[i])
  • Line 1 takes the input of the Phrase which needs to be converted into an acronym with the help of the input function.
  • Line 2 will 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 3 is initializing the final acronym with an empty string which will be changed later on.
  • From Line 4 and 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.
  • From Line 7 to Line 9 will display the final acronym that has been created with the help of loops and print statements.

Output Examples

The images below display some sample outputs after executing the code mentioned in the previous section.

Sample Output Acronym
Sample Output Acronym
Sample Output2 Acronym
Sample Output2 Acronym

Conclusion

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:

  1. Find Number of Possible Strings With No Consecutive 1s
  2. How to convert a dictionary to a string in Python?
  3. Convert a Tuple to a String in Python [Step-by-Step]
  4. String Formatting in Python – A Quick Overview