Computer Science

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

Python

Python String Manipulation

2 Likes

Answer

input_string = input("Enter the string: ")
words = input_string.split()
result = []

for word in words:
    if len(word) > 1:
        modified_word = word[0].upper() + word[1:-1] + word[-1].upper()
    else:
        modified_word = word.upper()
    result.append(modified_word)

capitalized_string = ' '.join(result)
print(capitalized_string)

Output

Enter the string: the quick brown fox jumps over the lazy dog                 
ThE QuicK BrowN FoX JumpS OveR ThE LazY DoG

Answered By

1 Like


Related Questions