CBSE Class 11 Computer Science Question 15 of 19

Strings in Python — Question 14

Back to all questions
14
Question

Question 13

Write a program that takes a sentence as an input parameter where each word in the sentence is separated by a space. The function should replace each blank with a hyphen and then return the modified sentence.

Solution
sentence = input("Enter a sentence: ")

modified_sentence = sentence.replace(' ', '-')

print(modified_sentence)
Output
Enter a sentence: Innovation drives progress
Innovation-drives-progress
Answer

sentence
=
input
(
"Enter a sentence: "
)
modified_sentence
=
sentence
.
replace
(
' '
,
'-'
)
print
(
modified_sentence
)
Output
Enter a sentence: Innovation drives progress
Innovation-drives-progress