Hit enter after type your search item

5 Examples to Learn Python String Replace Method

What is the replace method and how to use it?

The Python string replace method is used to “replace” the new substring with the existing substring in the specified string.

The replace method returns a copy of the string with replaced substring(s). The source string remains unchanged after using the replace method.

The source string remains unchanged after using the replace method.

Syntax for using replace method

This is how you may use the replace method with its parameters:

source_string.replace(old_substring, new_substring[, count])


  • The source string is the one you want to perform the replacement in.
  • The old_substring: The existing substring in the source string you want to replace.
  • The new_substring: The old_substring will be replaced by new_substring.
  • Count: This specifies the maximum number of replacements. If not provided, the replace Python method will replace all occurrences. The count is an optional parameter. Also, the replacements start from the left if the count parameter is specified. Alternatively, the first matches will be replaced (demonstrated in the example below).

An example of using Python replace method

In this example, a simple string is created. Another string variable is used which value is the returned value after using the replace method in the first string.

The source string is:

“A tutorial for learning Python replace function”

The word replace will be changed to “String replace”, as shown in the example below:

Python replace

See online demo and code

The code:


What if more than one match is found?

In this example, the following string is created:

src_str = “This is a test sentence with titlecase. This is a test sentence with titlecase. This is a test sentence with titlecase.”

By using the Python replace method, I will change “This” to “that” without specifying the count parameter. See what happens..

replace all

See online demo and code

The code:


In the output, you can see the word “this” is replaced in all occurrences to “that” i.e. three times. You may use the count parameter to limit the replacements. See next example.

An example of using the count parameter of replace method

Using the same string as in above example with the count parameter with value = 2. See the code and output:

replace count

See online demo and code

The code for using count function:


You can see, “this” is replaced only two times. The replacement is started from the left or only first matches are changed.

I want to replace the first occurrence from right, now what?

Unfortunately, the Python replace function will change from left to right only and there is no straightforward thing like “rreplace” etc. However, where there is a will there is a way.

The trick is using the join and rsplit methods together to get the result. In that case, specify the term to be changed in the join method and existing term in the rsplit method as shown in the example below:


string replace right

In the output, you can see “This” is changed to “That” from the right side. I used the 1 in the rsplit method, so it changed only one occurrence from the right side. You may question the performance as using both methods, however, this can be the way out.

Learn more about the Python join and rsplit methods.

Is the String replace function case sensitive?

Yes, the replace function is case sensitive. That means, the word “this” has a different meaning to “This” or “THIS”.

In the following example, a string is created with the different case letters, that is followed by using the Python replace string method.

string replace

See online demo and code

The code:


You can see, the source string contains “this”, “This” and “THIS”. In the replace string method, the replacement term is:

str_replaced = src_str.replace(“this”,”that”)

So, only the second occurrence is changed.

Need to perform case insensitive replacements?

The Python string replace method does not support case insensitive replacement. In order to perform a case-insensitive replacement, you may use the regular expression.

The re.IGNORECASE can do this job as shown in an example below.

string replace re.IGNORECASE

See online demo and code

The code:


You can see, the same string is used as in above replace method’s example. The regular expression’s sub method is used with the re.IGNORECASE.

In the output, you can see all occurrences of “this”, irrespective of the case are changed to “that”.

This div height required for enabling the sticky sidebar