CBSE Class 12 Computer Science
Question 32 of 57
Review of Python Basics — Question 32
Back to all questions 32
Question Write a Python program to capitalize first and last letters of each word of a given string.
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)Enter the string: the quick brown fox jumps over the lazy dog
ThE QuicK BrowN FoX JumpS OveR ThE LazY DoG
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