CBSE BOARD XII, asked by vicky534, 1 year ago

Write a Python program to capitalize first and last letters of each word of a given

Answers

Answered by yashgandhi74
8

Answer:

def capitalize(s):

s, result = s.title(), ""

for word in s.split():

result += word[:-1] + word[-1].upper() + " "

return result[:-1] #To remove the last trailing space.

print capitalize("i like cats")

Output


Sumanthsai313: hi
fiercespartan: .split() would not work in a problem like this
fiercespartan: and you will get the wrong output
Answered by fiercespartan
0

I recently solved this question on hackerrank, it is tricky but if you think, it is simple.

s = input()

def duplicates(lst, item):

       return [i for i, x in enumerate(lst) if x == item]

   s = list(s)

   space_indexes = duplicates(s,' ')

   s[0] = s[0].upper()

   for x in space_indexes:

       index = int(x)

       s[index+1] = s[index+1].upper()

   

   s = ''.join(s)

   print( s)

I basically checked for the indexes of the spaces in splitted list and then capitalized all the characters right after the space.

But, I used s[0] = s[0].upper() because the first word should always be capital.

Hope it helps! :)


rakeshchennupati143: st = input("Enter a sentence or string : ")
for word in st.split():
print(word[0].upper()+word[1:-1]+word[-1].upper(), sep=' ', end=' ')

---Simple isn't it?
rakeshchennupati143: st = input("Enter a sentence or string : ")
for word in st.split():
if word > 1: print(word[0].upper()+word[1:-1]+word[-1].upper(), sep=' ', end=' ')
rakeshchennupati143: else
rakeshchennupati143: print(word[0].upper())
fiercespartan: We could do this:

Sentence = input()
For x in sentence.split():
Sentence = sentence.replace(x,x.capitalize())
Print(sentence)
fiercespartan: I could do it much simpler ways ways man.. I just need people to know python to the fullest. So I use many high end stuff to solve it
rakeshchennupati143: OMG LOL, that was too good
rakeshchennupati143: ok have you checked the link i gave you in insta?
fiercespartan: Yes
rakeshchennupati143: check insta DM
Similar questions