Remember
replace() returns text with matching substrings replaced. A count limits the number of replacements.
replace() returns text with matching substrings replaced. A count limits the number of replacements.
Follow the example, change its sample, and compare what happens.
Explain the result and test your understanding.
Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.
Follow the code and watch its state change. Guided simulation.
text = 'tea tea'
result = text.replace("tea", "coffee", 1)
print(result)Top-level scope
'tea tea'Object 2 · str'coffee tea'replace() returns text with matching substrings replaced. A count limits the number of replacements.
(no output)
What do you expect this program to print?
EXAMPLE INSTRUCTIONS
text = 'tea tea'
result = text.replace("tea", "coffee", 1)
print(result)
|
v
DISPLAYED RESULT
coffee tea
FINAL BINDINGS
text --> object 1 (str)
result --> object 2 (str)
object 1: 'tea tea'
object 2: 'coffee tea'Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.
Choose a sample and follow its execution. Guided simulation.
text = 'tea tea'
result = text.replace("tea", "coffee", 1)
print(result)Top-level scope
'tea tea'Object 2 · str'coffee tea'The example has finished. Compare the final output with the values and references above.
coffee tea
What do you expect this program to print?
text = 'tea tea'
result = text.replace("tea", "coffee", 1)
print(result)replace() MethodThe replace() method in Python is used to replace parts of a string with another string. It’s a very useful and beginner-friendly tool for text processing.
string.replace(old, new, count)
old – The substring you want to replace.new – The substring you want to replace it with.count (optional) – The number of times you want to replace old with new. If omitted, all occurrences are replaced.text = "I love apples"
new_text = text.replace("apples", "bananas")
print(new_text)
I love bananas
quote = "ha ha ha!"
print(quote.replace("ha", "ho"))
ho ho ho!
data = "one one one"
print(data.replace("one", "two", 1))
two one one
replace(",", "") to remove commas)replace() returns a new string – it does not change the original."hello".replace("H", "J") does nothing.Use replace() when cleaning strings or doing simple data transformations. It’s often seen in string manipulation questions or during preprocessing.
replace() is used to replace parts of a string with a new value.count parameter.Write a program to replace all spaces in a sentence with dashes -.
sentence = input("Enter a sentence: ")
print(sentence.replace(" ", "-"))
Connect what you noticed in the example with the idea below.
replace() returns text with matching substrings replaced. A count limits the number of replacements.
replace() modifies the original string in place.
This is the misconception to avoid.Reference checks are optional and do not count toward course progress.