Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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
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
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
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
Hint
Use print(new_story) to show the changed story.
Practice
(1/5)
1. What does the replace method do in Python strings?
easy
A. It finds a specified substring and replaces it with another substring.
B. It deletes a substring from the string permanently.
C. It changes the original string directly without creating a new one.
D. It counts how many times a substring appears in the string.
Solution
Step 1: Understand the purpose of replace()
The replace method searches for a substring and replaces it with another substring.
Step 2: Check if it modifies original string
Strings are immutable in Python, so replace returns a new string without changing the original.
Final Answer:
It finds a specified substring and replaces it with another substring. -> Option A
Quick Check:
replace() = find and change substring [OK]
Hint: Remember: replace returns new string, original stays same [OK]
Common Mistakes:
Thinking replace changes the original string
Confusing replace with delete or count methods
Assuming replace modifies in-place
2. Which of the following is the correct syntax to replace all occurrences of 'cat' with 'dog' in a string text?
easy
A. text.replace('cat', 'dog', all=True)
B. text.replace('cat', 'dog')
C. text.replace('cat' -> 'dog')
D. text.replace('cat', 'dog', count=0)
Solution
Step 1: Recall replace() syntax
The correct syntax is string.replace(old, new[, count]), where old and new are strings.
Step 2: Analyze options
text.replace('cat', 'dog') matches the correct syntax. Options A, B, and D use invalid syntax or parameters.
Hint: Use replace(old, new) without extra keywords [OK]
Common Mistakes:
Using arrows or keywords inside replace()
Adding unsupported parameters like all=True
Confusing parameter order
3. What will be the output of this code?
text = 'apple apple apple'
new_text = text.replace('apple', 'orange', 2)
print(new_text)
medium
A. 'apple orange orange'
B. 'orange orange orange'
C. 'orange orange apple'
D. 'apple apple orange'
Solution
Step 1: Understand replace with count
The third argument 2 limits replacements to first two occurrences.
Step 2: Apply replacements
First two 'apple' become 'orange', last remains 'apple'. Result: 'orange orange apple'.
Final Answer:
'orange orange apple' -> Option C
Quick Check:
replace with count=2 changes first two only [OK]
Hint: Count limits how many replacements happen [OK]
Common Mistakes:
Replacing all occurrences ignoring count
Replacing from the end instead of start
Miscounting number of replacements
4. The following code tries to replace 'blue' with 'red' but causes an error. What is the error?
text = 'blue sky'
text.replace('blue', 'red', 'all')
print(text)
medium
A. SyntaxError due to wrong quotes
B. AttributeError because replace is not a string method
C. No error, prints 'red sky'
D. TypeError because 'all' is not an integer for count
Solution
Step 1: Check replace() parameters
The third parameter must be an integer count, but 'all' is a string.
Step 2: Identify error type
Passing a string instead of int causes a TypeError at runtime.
Final Answer:
TypeError because 'all' is not an integer for count -> Option D
Quick Check:
replace count must be int, not string [OK]
Hint: Count parameter must be integer, not string [OK]
Common Mistakes:
Using strings instead of integers for count
Expecting replace to modify original string
Confusing error types
5. You have a paragraph stored in text. You want to replace only the first 3 occurrences of the word 'error' with 'issue'. Which code snippet correctly does this?
hard
A. new_text = text.replace('error', 'issue', 3)
B. new_text = text.replace('error', 'issue', count=3)
C. new_text = text.replace('error', 'issue', '3')
D. new_text = text.replace('error', 'issue')[:3]
Solution
Step 1: Use replace with count parameter
To replace only first 3 occurrences, use replace(old, new, count) with count=3.