0
0
Pythonprogramming~15 mins

Searching and replacing text in Python - Mini Project: Build & Apply

Choose your learning style9 modes available
Searching and replacing text
๐Ÿ“– Scenario: Imagine you have a short story saved as a string. You want to find and replace certain words to make the story more fun or correct mistakes.
๐ŸŽฏ Goal: You will create a program that searches for a specific word in a story and replaces it with another word.
๐Ÿ“‹ What You'll Learn
Create a string variable with the story text
Create variables for the word to find and the word to replace it with
Use the string replace() method to change the word
Print the updated story
๐Ÿ’ก Why This Matters
๐ŸŒ Real World
Text editing and processing is common in writing apps, chat programs, and data cleaning.
๐Ÿ’ผ Career
Knowing how to search and replace text helps in software development, data analysis, and content management.
Progress0 / 4 steps
1
Create the story text
Create a string variable called story with this exact text: 'The cat sat on the mat.'
Python
Need a hint?

Use single or double quotes to create the string.

2
Set the word to find and replace
Create a variable called word_to_find and set it to 'cat'. Then create a variable called word_to_replace and set it to 'dog'.
Python
Need a hint?

Use simple assignment to create these variables.

3
Replace the word in the story
Create a new variable called new_story that uses story.replace(word_to_find, word_to_replace) to replace the word.
Python
Need a hint?

Use the replace() method on the story string.

4
Print the updated story
Write a print() statement to display the new_story variable.
Python
Need a hint?

Use print(new_story) to show the changed story.