Reputation: 13
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.
Upvotes: -1
Views: 81
Reputation: 13
# 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
Upvotes: 0
Reputation: 27760
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'
Upvotes: 0