Editor

Python Program to Make an Acronym from a Phrase

Beginner · Strings

An acronym is a short word made from the first letters of a phrase, like PNG from Portable Network Graphics or ISRO from Indian Space Research Organisation. This program takes the first letter of every word and joins them in capitals.

Program

acronym.py
phrase = "Portable Network Graphics" # or: phrase = input("Enter a phrase: ")
acronym = ""
for word in phrase.split():
acronym += word[0].upper() # first letter, as a capital
print("Phrase: ", phrase)
print("Acronym:", acronym)
Output
Phrase:  Portable Network Graphics
Acronym: PNG

How it works

  1. phrase.split() gives the list of words in the phrase.
  2. word[0] is the first letter of each word, and upper() makes it a capital, so phrases in small letters work too.
  3. Each letter is added to the end of acronym, building the result one letter at a time.
  4. split() ignores extra spaces, so there are never empty words that would break word[0].

Other ways to write it

Using a comprehension and join()

Using a comprehension and join()
phrases = ["as soon as possible", "frequently asked questions", "Central Processing Unit"]
for phrase in phrases:
acronym = "".join(word[0].upper() for word in phrase.split())
print(phrase, "->", acronym)
Output
as soon as possible -> ASAP
frequently asked questions -> FAQ
Central Processing Unit -> CPU

The comprehension picks the first letter of every word, and "".join() glues the letters together.

Skipping small words

Skipping small words
small_words = ["of", "and", "the", "for", "in"]
for phrase in ["Indian Institute of Technology", "Department of Science and Technology"]:
acronym = ""
for word in phrase.split():
if word.lower() not in small_words:
acronym += word[0].upper()
print(phrase, "->", acronym)
Output
Indian Institute of Technology -> IIT
Department of Science and Technology -> DST

Real acronyms usually leave out words like "of" and "and", so the program skips any word in small_words.

Try it yourself

Make the program print the acronym with a full stop after each letter, like P.N.G.

Edit the program above and press Run, or open it in the full editor to save it and share it. To watch it run line by line, open it in the visualizer.

All Python programs · CBSE class 12 practical file · Python exercises