In: Computer Science
IN PYTHON
Write a function capitalize_last(phrase) that takes in a string phrase consisting of arbitrarily capitalized words separated by spaces, and returns a new string consisting of those words but with the only the last letter in each word uppercase.
You can assume that the string contains only letters and spaces, no punctuation.
Examples:
>>> capitalize_last('tEst WiTH rANdoM capITaliZATioN')
'tesT witH randoM capitalizatioN'
>>> capitalize_last('')
''
>>> capitalize_last('i am the senate')
'I aM thE senatE'
The program is well commented, even if you face any doubt leave a comment.
Program:
#defining function
def capitalize_last(phrase):
#first of all we'll convert the whole string into lower case
phrase = phrase.lower()
#then we'll split the string so that we could access each word
words = phrase.split()
#then we'll captalize the last character
for i in range(len(words)):
words[i] = words[i][:-1] + words[i][-1].upper()
#lastly we'll concate the whole words
phrase = " ".join(words)
#we'll return the phrase
return phrase
#Main program
#taking phrase from the user
phrase = input("Enter the phrase: ")
#calling the function
ans = capitalize_last(phrase)
#printing the output
print("Converted phrase: '{}'".format(ans))
Output:
1.
2.
3.
Leave a comment if face any doubt!!!