Python Program to Convert Text to Acronyms

What is Acronym?

An acronym is a shorter version(short form) of a longer phrase; for example, ML stands for Machine Learning.

Let us see how the Python application converts text to acronyms.

While saying or writing the first initial of each word or an abbreviated form of the complete word takes longer, saying or writing the first initial of each word or an abbreviated form of the entire word takes less time. As a result, using acronyms and abbreviations in everyday speech facilitates communication.

Our goal in implementing acronyms is to construct a short form of a word from a given text. The same thing can be accomplished by splitting and indexing to obtain the first word and then combining it.

Examples:

Example1:

Input:

Given string = "Artificial Intelligence"

Output:

The Acronym for the given string =  AI
A  stands for -  Artificial
I  stands for -  Intelligence

Example2:

Input:

Given string = "Indian Space Research Organization"

Output:

The Acronym for the given string = ISRO
I stands for - Indian
S stands for - Space
R stands for - Research
O stands for - Organization

Program to Convert Text to Acronyms in Python

Method #1: Using For loop (Static Input)

Approach:

  • Give the string as static input and store it in a variable.
  • Split the given string into a list of words using the split() function and store it in another variable.
  • Take a variable and initialize it with an empty string.
  • Loop in the above word list using the for loop.
  • Inside the loop, convert the first character of the iterator into an upper case using the upper() function and concatenate it with the above declared empty string.
  • Store it in the same variable.
  • Print the Acronym for the given string.
  • Loop till the length of the above-obtained result acronym string using the for loop.
  • Inside, the loop print the iterator value of result acronym string and word list separated by spaces.
  • The Exit of the Program.

Below is the implementation:

# Give the string as static input and store it in a variable.
gvn_str = "Artificial Intelligence"
# Split the given string into a list of words using the split() function and
# store it in another variable.
word_lst = gvn_str.split()
# Take a variable and initialize it with an empty string.
rslt_acrnym = ""
# Loop in the above word list using the for loop.
for itr in word_lst:
    # Inside the loop, convert the first character of the iterator into an upper
        # case using the upper() function and concatenate it with the above declared
        # empty string.
    # Store it in the same variable.
    rslt_acrnym += itr[0].upper()

# Print the Acronym for the given string.
print("The Acronym for the given string = ", rslt_acrnym)
# Loop till the length of the above-obtained result acronym string using the for loop.
for itr in range(len(rslt_acrnym)):
    # Inside, the loop print the iterator value of result acronym string and word list
        # separated by spaces.
    print(rslt_acrnym[itr], " stands for - ", word_lst[itr])

Output:

The Acronym for the given string =  AI
A  stands for -  Artificial
I  stands for -  Intelligence

Method #2: Using For loop (User Input)

Approach:

  • Give the string as user input using the input() function and store it in a variable.
  • Split the given string into a list of words using the split() function and store it in another variable.
  • Take a variable and initialize it with an empty string.
  • Loop in the above word list using the for loop.
  • Inside the loop, convert the first character of the iterator into an upper case using the upper() function and concatenate it with the above declared empty string.
  • Store it in the same variable.
  • Print the Acronym for the given string.
  • Loop till the length of the above-obtained result acronym string using the for loop.
  • Inside, the loop print the iterator value of result acronym string and word list separated by spaces.
  • The Exit of the Program.

Below is the implementation:

# Give the string as user input using the input() function and store it in a variable.
gvn_str = input("Enter some random string = ")
# Split the given string into a list of words using the split() function and
# store it in another variable.
word_lst = gvn_str.split()
# Take a variable and initialize it with an empty string.
rslt_acrnym = ""
# Loop in the above word list using the for loop.
for itr in word_lst:
    # Inside the loop, convert the first character of the iterator into an upper
        # case using the upper() function and concatenate it with the above declared
        # empty string.
    # Store it in the same variable.
    rslt_acrnym += itr[0].upper()

# Print the Acronym for the given string.
print("The Acronym for the given string = ", rslt_acrnym)
# Loop till the length of the above-obtained result acronym string using the for loop.
for itr in range(len(rslt_acrnym)):
    # Inside, the loop print the iterator value of result acronym string and word list
        # separated by spaces.
    print(rslt_acrnym[itr], " stands for - ", word_lst[itr])

Output:

Enter some random string = Indian Space Research Organization
The Acronym for the given string = ISRO
I stands for - Indian
S stands for - Space
R stands for - Research
O stands for - Organization