Python String replace() Method: Explanation with Example Program

Strings in Python are arrays of bytes that represent Unicode characters.

You may use square brackets to access elements of a string and there are various possible methods for the string object.

Commonly used functions include lower(), upper(), join(), split(), find(), and replace() string.

What is a replace() string method in Python?

The replace() method substitutes the new character/text for each occurrence of the old character/text in the string.

Syntax:

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

Parameters:

The replace() method may accept up to three parameters:

  • old – old substring you want to replace
  • new – new substring which will replace the old substring
  • count (optional) – the number of times you want to replace the old substring with the new substring

Return value:

This method provides a duplicate of the string in which all instances of the substring old have been replaced with new.

Only the first count instances are changed if the optional parameter max is used.

If the old substring is not found, the original string is returned.

Example Program:

s = 'PHP is Nice'

str_new = s.replace('PHP', 'Python')
print(str_new)

The program above is a simple string replace and replace character in string.

The string ‘PHP is replaced by a new string Python, and it will print Python is Nice.

Output:

Python is Nice

What does replace() do in Python?

Using the Python .replace() function, you may replace every occurrence of a certain character with another.

You may even replace a whole string of text with a specified new line of text.

The .replace() function returns a string copy. This indicates that the original substring stays unchanged, but a new copy is generated with all of the old content replaced with the new text.

Can you replace a string in Python?

Python’s replace() method produces a string in which all instances of a substring have been replaced by another substring.

How do I replace a string in Python 3?

The replace() method of Python 3 returns a copy of the string in which the occurrences of old have been replaced with new, optionally restricting the number of replacements to max.

Syntax for replace() method:

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

Parameters

  • old − This is an old substring that has to be replaced.
  • new − This is a new substring, which would replace the old substring.
  • max −This is optional. If max is given, just the first count instances are replaced.

How do you replace two words in Python?

We can replace two words in Python by using the optional third parameter count that .replace() accepts.

By default, .replace() will replace all instances of the substring.

However, you can use count to specify the number of occurrences you want to be replaced.

phrase = "I like to learn coding on the go"

substituted_phrase = phrase.replace("o", "a", 2 )

print(phrase)
print(substituted_phrase)

Output:

I like to learn coding on the go
I like ta learn cading on the go

Example programs using replace() string in Python

Example program 1:

string = "python for free free free"

print(string.replace("free", "Free!"))

print(string.replace("for", "forFree!", 2))

Output:

python for Free! Free! Free!
python forFree! free free free

 

Example program 2:

string = "python for free free free free"

print(string.replace("e", "a"))

print(string.replace("ek", "a", 3))


  

Output:

python for fraa fraa fraa fraa
python for free free free free

Conclusion

In summary, learning Python replace() string with its well-explained example programs is very useful in Python programming.

It is a method to perform substring substitution and performs case-insensitive substring substitution.

Aside from this topic, you may also check: How to Convert String Cases in Pandas with Examples

Leave a Comment