Pyton

Python String replace() Function

Python String replace() Function

String replacement is often essential. If you want to replace any string or word in your program, then one option is to manually check the whole program and replace each string with the desired string. Python also provides a built-in replace() function for string replacement. The Python replace() function does not replace the actual string, but it makes a copy of the string, and replaces instances of the specified string with the new string. This article shows you how to use the replace() function in Python.

Syntax

The syntax of the replace() function is as follows:

string.replace(oldstring, newstring,count)

Old String: The string that you want to replace.

New String: The string that replaces the old string.

Count: This parameter is optional. The count parameter is used to state the number of times you wish to replace the old string with the new string.

The replace() function only returns the copy of the string.

Examples

We will now look at some examples of the Python replace() function. In the example given below, we will replace the term “website” with the term “linuxhint.”

# declaring the original string
str="Hello and welcome to the website"
# replacing the "website" with "linuxhint"
print("The replaced string is: ",str.replace("website","linuxhint"))

Output

The output is displayed in the Python console. This output shows that the term “website” has been replaced with the term “linuxhint.”

Let us see another example of the replace() function. Here, we will replace the term “dog” with the term “cat.” The count value is 1, which indicates that the term “dog” will be replaced with the term “cat” only once in the string.

# declaring the original string
str="Dog is an animal. Dog eat food"
# replacing the "Dog" with "Cat"
print(str.replace("Dog","Cat",1))

Output

The output is displayed in the Python console. This output shows that the first term “dog” has been replaced with the term “cat” in the string.

If the count value were 2, then the function would replace the first two occurrences of the term “dog” with the term “cat” in the string. If you do not use a count value, then the replace() function replaces all instances of the specified old_string with the chosen new_string.

# declaring the original string
str="Dog is an animal. Dog eat food"
# replacing the "Dog" with "Cat"
print(str.replace("Dog","Cat",2))

Output

The output is displayed in the Python console. This output shows that the two occurrences of the term “dog” have been replaced with the term “cat” in the string.

As discussed earlier, the replace() function only returns a copy of the original string. It does not change the original string. We will now print the original string after replacing the term “dog” with the term “cat.”

# declaring the original string
str="Dog is an animal. Dog eat food"
# replacing the "Dog" with "Cat"
print("Replaced string: ",str.replace("Dog","Cat",2))
# printing the original string
print("Original String: ",str)

Output

The output is displayed in the Python console. This output shows that the original string remained the same. The replace() function only returns the copy of the original string after making the changes.

Conclusion

This article explains string replacement in Python using the replace() function with the help of some simple examples. The article should have helped beginners to learn more about performing string replacement in Python using the replace() function.

Gry SuperTuxKart for Linux
SuperTuxKart for Linux
SuperTuxKart is a great title designed to bring you the Mario Kart experience free of charge on your Linux system. It is pretty challenging and fun to...
Gry Battle for Wesnoth Tutorial
Battle for Wesnoth Tutorial
The Battle for Wesnoth is one of the most popular open source strategy games that you can play at this time. Not only has this game been in developmen...
Gry 0 A.D. Tutorial
0 A.D. Tutorial
Out of the many strategy games out there, 0 A.D. manages to stand out as a comprehensive title and a very deep, tactical game despite being open sourc...