Python String replace() Method

This Python tutorial will teach us how to use the Python 3 String replace() method. Moreover, we will also discuss multiple examples of how to define and use the replace() method with Python String.

The replace() is a built-in string method in Python that allows changing the part of a given string with something else.

Let us understand the replace() method in more detail. Moreover, we will discuss the following topics.

  • Python 3 String Replace() Method
  • Python 3 String Replace() Method Syntax
  • Python 3 String Replace() Method Return Value
  • Python 3 String Replace() Method Examples

Python 3 String Replace() Method

The replace() method in Python is a built-in string method that replaces all occurrences of a given substring in the original string with some new string and returns it.

For example, imagine we have the string “Hello, World!“. And we could use the replace() method to change the word “World” to “Python“.

So, initially, we had the string as “Hello, World!” and after using replace() method, we will get “Hello, Python!” as a result.

Python 3 String Replace() Method Syntax

Now that we understood what is replace() method in Python, let us discuss the syntax of using the replace() method.

string.replace(old, new[, count])

In the above syntax:

  • string: It is the original string from which we want to replace the string.
  • old: It is a mandatory argument used to specify the substring that we want to replace from the original one.
  • new: It is the mandatory argument used to specify the substring that we want to replace the old with.
  • [count]: is an optional argument that specifies the maximum number of occurrences of old to be replaced. If we omit this argument, all occurrences of old will be replaced.

Read: Python string formatting

Python 3 String Replace() Method Return Value

The string.replace() method produces a copy of the string that has all instances of the substring old replaced with the new substring.

Moreover, the first count instances are the only ones that are replaced if the optional argument count is used.

Read: Python compare strings

Python 3 String Replace() Method Examples

Now that we understood the syntax and working of the Python 3 String replace() method, let us look at the examples of using it.

Example 1: Replacing all occurrences of a single character

First, let us first look at an example where we will replace a single character with a new character from all its occurrences.

# Define the string
msg = "The quick brown fox jumps over the lazy dog."

# Replace "United States" with "USA"
new_string = msg.replace("o", "x")

# Printing origianl string
print("Original String:", msg)

# Print the new string
print("New String:",new_string)
  • In this example, we have taken a string value containing the “o” character at multiple instances.
  • However, we are using the replace() method in Python to replace the character “o” with “x” from all its occurrences.

Once we execute the Python program, we will get the following result.

Original String: The quick brown fox jumps over the lazy dog.
New String: The quick brxwn fxx jumps xver the lazy dxg.

Example 2: Replacing substring from all occurrences

Next, let us first look at an example where we will replace a substring with a new substring from all its occurrences.

# Define the string
msg = "The United States is a federal republic, but United States citizens can vote in elections."

# Replace "United States" with "USA"
new_string = msg.replace("United States", "USA")

# Printing origianl string
print("Original String:", msg)

# Print the new string
print("New String:",new_string)

In the above example, we have a string named msg containing some “United States” substring. So, we are using the replace() method on the msg string to replace the “United States” substring with “USA“.

Moreover, as we didn’t specify the count argument, the replace() method will replace the substring from all its occurrences.

The result of the above Python program is shown below.

Original String: The United States is a federal republic, but United States citizens can vote in elections.
New String: The USA is a federal republic, but USA citizens can vote in elections.

Example 3: Replacing substring from n occurrences

In the previous example, we have seen how to replace a substring from all its occurrences. In this example, we will see how to replace the substring from its n occurrences. And n here could be any integer value.

Here is an example in Python

# Define the string
msg = "The United States is a federal republic, but United States citizens can vote in elections."

# Replace "United States" with "USA" from 1 instance
new_string = msg.replace("United States", "USA", 1)

# Printing origianl string
print("Original String:", msg)

# Print the new string
print("New String:",new_string)

In the example, the replace() function will only replace the United States substring from its first occurrence.

So, after the execution of the Python program, we will get the following result.

Python 3 String Replace
Using string.replace() in Python

Example 4: Python remove all instances of a character from a string

  • In this section, we will learn how to remove all instances of a character from a string in Python.
  • String replace() method replaces a character from a string in the list.
  • For example, removing all instances of “a” in “California” we have to use the string replace() function.

Syntax:

Here is the Syntax of String replace.

replace[
        
        old_Str2,
        new_Str3,
        instance
       ]

Let’s take an example to check how to remove all instances of a character from a string.

str2="calfornia"
str3= str2.replace("a","")
print(str3)

Here is the screenshot of the following given code.

Remove all instances of a character from a string

This is how to remove all instances of a character from a string in Python.

So, in this section, we have gone through multiple examples of how to use the string.replace() method in Python 3.

You may also like to read the following Python tutorials.

Conclusion

In this Python tutorial, we understood what is Python 3 string replace() method is. Moreover, we have also discussed the syntax of using it. Additionally, we covered multiple examples related to it.

Here is the list of topics that we covered.

  • Python 3 String Replace() Method
  • Python 3 String Replace() Method Syntax
  • Python 3 String Replace() Method Return Value
  • Python 3 String Replace() Method Examples