Acronym in Python

PythonServer Side ProgrammingProgramming

Suppose we have a string s that is representing a phrase, we have to find its acronym. The acronyms should be capitalized and should not include the word "and".

So, if the input is like "Indian Space Research Organisation", then the output will be ISRO

To solve this, we will follow these steps −

  • tokens:= each word of s as an array

  • string:= blank string

  • for each word in tokens, do

    • if word is not "and", then

      • string := string concatenate first letter of word

  • return convert string into uppercase string

Let us see the following implementation to get better understanding −

Example

 Live Demo

class Solution:
   def solve(self, s):
      tokens=s.split()
      string=""
      for word in tokens:
         if word != "and":
            string += str(word[0])
      return string.upper()
ob = Solution()
print(ob.solve("Indian Space Research Organisation"))

Input

"Indian Space Research Organisation"

Output

ISRO
raja
Published on 02-Sep-2020 12:41:10
Advertisements