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
Phrase: Portable Network Graphics Acronym: PNG
How it works
phrase.split()gives the list of words in the phrase.word[0]is the first letter of each word, andupper()makes it a capital, so phrases in small letters work too.- Each letter is added to the end of
acronym, building the result one letter at a time. split()ignores extra spaces, so there are never empty words that would breakword[0].
Other ways to write it
Using a comprehension and join()
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
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.
Related programs
All Python programs · CBSE class 12 practical file · Python exercises