Python String replace() Method | Explained

To store any sequence data, such as the alphabet sequence, numbers, or characters, the “string” data type is used in Python. Various built-in functions are available in Python to perform operations like replacing substrings, removing substrings, etc.

This write-up will provide you with a detailed understanding of the Python string “replace()” method with the following aspects:

So let’s get started!

What is the Python String replace() Method?

The Python string “replace()” method replaces the old substring value with a new value in any given string. We can control the replacement of multiple substrings in a given string by using the “count” occurrence parameter.

How to install Kali Linux in Window...

The syntax of the Python “replace()” method is shown below:

string.replace(oldvalue, newvalue, count)

In the above syntax:

  • The “oldvalue” parameter takes a value that needs to be replaced.
  • The “newvalue” parameter takes a value that will replace the old value.
  • The “count” parameter takes an integer value. The default value will be set to “all occurrences”. This parameter specifies how many times the old value will be replaced with a new value in a given string.

The following examples demonstrate the working of the “replace()” method:

Example 1: Simple replace() Method

In the example given below, the “replace()” method is used to replace the substring value with a new value.

Code:

#Using replace()
string_value = "Python Guide"

result = string_value.replace("Python", "Linux")

print(result)

In the above code:

  • The string value is initialized in a variable named “string_value”.
  • The “replace()” method takes the old and new values as a parameter and replaces the old value of the string with a new value.

Output:

The above output verified that the old value “Python” had been replaced with the new value “Linux”.

Example 2: Using replace() Method to Replace Multiple Same Values

In the example given below, the “replace()” method replaces multiple occurrences of a substring in the given string.

Code:

#Using replace()
string_value = "Python Guide and Python Tutorial"
result = string_value.replace("Python", "Linux")

print(result)

In the above code:

  • The “string” value with multiple same substring “Python” values is initialized.
  • The “replace()” method takes the old value “Python” and replaces it with a new value “Linux” for all occurrences in the given string.
  • Here, keep in mind that the value of the third parameter, “count”, is not passed so that the replacement will be done for all the occurrences of substrings.

Output:

In the above output, the new value “Linux” replaces the old value “Python” for all occurrences.

Example 3: Using replace() Method to Replace Multiple Value With Occurrence Parameter

In the example given below, the “replace()” method replaces the first two occurrences of the same substring.

Code:

#Using replace()
string_value = "Python Guide and Python Tutorial, Python Programming"
result = string_value.replace("Python", "Linux",2)

print(result)

In the above code:

  • The “string” value with multiple same substring “Python” values is initialized.
  • The “replace()” method takes the old value “Python” and replaces it with a new value “Linux” for the first two occurrences in the given string. This is because the third parameter value is set to “2”, which means the first two occurrences of the old value will be replaced with the new value in the given string.

Output:

The above output replaces the first two occurrences of “Python” with “Linux”.

That’s all from this guide!

Conclusion

The inbuilt “replace()” method in Python replaces the old substring value with a new one. This method will replace all the occurrences by default. Specify a “count” parameter to replace only a specific number of occurrences in the given string. This Python guide presented a detailed overview of the string “replace()” method with numerous examples.