Home » Python » Python String replace() Method

Python String replace() Method

replace() method in Python is a built-in function that returns the string in which all occurrences of substring old are replaced by new value.

If an optional parameter count is provided, it will limit the string replacements to count.

In this tutorial, I will explain how to use the Python String replace() method with examples.

replace() Syntax

The syntax for the replace() method is:

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

replace() Parameters

The replace() method has three arguments

  • old– Required parameter. The String to replace for.
  • new– Required parameter. String value to replace the old value with a new value
  • count– Optional. An Integer. A number of occurrences of the old value are to be replaced by the new value. By default, it replaces all occurrences of old values with a new value.

replace() Return Value

The Python string replace() method returns a string.

Let’s understand the string package replace() method with examples.

Replace all occurrences of old value with new

The following Python program replace all occurrences of a substring old with a new value using the Python string replace() method.

str = "This is string replace method example. replace method return copy of string with occurances of substring replaced by new value"
str1 = str.replace("replace","replace()")
print (str1)

In the above Python program, we have a string stored in the str variable.

Using the replace() method on the str to replace the old value with the new value. It returns the copy of the string that is stored in the str1 variable.

Use the print() method to print the string.

The output of the above Python program is

This is string replace() method example. replace() method return copy of string with occurances of substring replace()d by new value

Replace the first two occurrences of substring in string

replace() method has a count parameter as an optional parameter that restricts the occurrences of replacement of old value with new value in the specified string.

The following program demonstrates the replacement of the first two occurrences of an old string with a new value.

str = "This is string replace method example. replace method return copy of string with occurances of substring replaced by new value"
str1 = str.replace("replace","replace()",2)
print (str1)

In the above Python program, replace() method takes three parameters as old value, new value, and count parameter as 2 to limit the replacements.

Call the replace() method on an str variable to replace the old value with a new value in the string for the first 2 occurrences only.

Use the print() method to print the string.

Conclusion

I hope the above article on the replace() method to replace the old value with a new value in a string is helpful to you.

If you don’t specify the count parameter, all occurrences of the specified old value will be replaced by a new value.

You can find more topics about the Python tutorials on the DataVisualizr Home page.

Leave a Comment