sentence = "I eat apples"
new_sentence = sentence.replace("apples", "bananas")
print(new_sentence)
#source code --> clcoding.com
Explanation:
Define the Variable:
sentence = "I eat apples" stores the original sentence in the variable sentence.
Use .replace() to Change the Word:
The .replace("apples", "bananas") method finds the word "apples" and replaces it with "bananas".
The new sentence becomes "I eat bananas".
.replace() does not modify the original string; it creates a new modified string.
Print the Updated Sentence:
print(new_sentence) outputs the modified sentence.
Output:
I eat bananas
0 Comments:
Post a Comment