Write a python script that generates an acronym word from a given sentence

81 Views Asked by At

Basically we have to make an acronym word where we have to generate short form from a long form sentence.

I tried to run the code but it was showing error while taking the string from the input.

2

There are 2 best solutions below

0
On BEST ANSWER
def acronym(s):
    return ''.join([x[0] for x in s.split(' ')])

#here split(' ') will break all the words into a list.
#x[0] will take the first letters of all words.
#finally join will again convert it to a string from a list

print(acronym('The State Department'))
'TSD'
2
On
# function to create acronym
def fxn(stng):
    # add first letter
    oupt = stng[0]

    # iterate over string
    for i in range(1, len(stng)):
        if stng[i - 1] == ' ':
            # add letter next to space
            oupt += stng[i]

    # uppercase oupt
    oupt = oupt.upper()
    return oupt