Summary: in this tutorial, you’ll learn how to use the Python string replace()
method to replace some or all occurrences of a substring with a new substring.
Introduction to the Python string replace() method
The replace()
method returns a copy of a string with some or all matches of a substring replaced with a new substring.
The following shows the syntax of the replace()
method:
str.replace(substr, new_substr [, count])
Code language: CSS (css)
The replace()
method accepts three parameters:
substr
is a string that is to be replaced by thenew_substr
.new_substr
is the string that replaces thesubstr
count
is an integer that specifies the firstcount
number of occurrences of the substr that will be replaced with thenew_substr
. Thecount
parameter is optional. If you omit the count argument, thereplace()
method will replace all the occurrences of thesubstr
by thenew_substr
.
It’s important to note that the replace()
method returns a copy of the original string with some or all occurrences of the substr
replaced by the new_substr
. It doesn’t change the original string.
Python string replace() method examples
Let’s take some examples of using the replace()
method.
1) Replacing all occurrences of a substring with a new substring
The following example uses the string replace() method to replace all occurrences of the substring 'We'
by the new substring 'Python'
:
s = 'We will, We will rock you!'
new_s = s.replace('We', 'Python')
print(s)
print(new_s)
Code language: PHP (php)
Output:
We will, We will rock you!
Python will, Python will rock you!
2) Replacing some occurrences of a substring by a new substring
The following example uses the replace()
method to replace the first occurrence of the substring 'bye'
by the new substring 'baby'
:
s = 'Baby bye bye bye!'
new_s = s.replace('bye', 'baby', 1)
print(new_s)
Code language: PHP (php)
Output:
Baby baby bye bye!
In this example, we passed the count argument as one. Therefore, the replace() method just replaces the first occurrence of the string 'bye'
with the string 'baby'
Summary
- Use the Python string
replace()
method to return a copy of a string with some or all occurrences of a substring replaced by a new substring.