Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

python
community creator

How to find the acronym of a string

Aarthi Reddy

What is an acronym?

An acronym is an abbreviation formed from the initial letters of the words in a string.

Note: Connectives like or, and, and of are not used in abbreviation.

Examples

  • WHO: World Health Organization

  • ISRO: Indian Space Research Organization

svg viewer
Indian Space Research Organization acronym is ISRO

Step-1

Find the first letter in the words of the given string.

Step-2

Join all the letters and print it as an abbreviation.

Code

s = "national aeronautics and space administration"
s1 = s.split()
l = []
for i in s1:
    if(i != "and"):
        a = i[0]
        l.append(a)
b = "".join(l)
print(b.upper())    
  • The given string is split to find the first letter in each word.

  • The split() method splits a string into a list – the default separator is whitespace.

  • An empty list is taken to store all the first letters of the words in the given string.

  • The join() method takes all items in an iterable and joins them into one string.

  • Finally, the upper method is used to convert all the characters in the string to uppercase.

RELATED TAGS

python
community creator
RELATED COURSES

View all Courses

Keep Exploring