replace() is an inbuilt function in Python programming language that returns a copy of the string where all occurrences of a substring is replaced with another substring.
Syntax :
string.replace(old, new, count)
Parameters :
old – old substring you want to replace.
new – new substring which would replace the old substring.count – the number of times you want to replace the old substring with the new substring. (Optional )
Return Value :
It returns a copy of the string where all occurrences of a substring is replaced with another substring.
Below is the code demonstrating replace() :
# Python3 program to demonstrate the # use of replace() method string = "geeks for geeks geeks geeks geeks" # Prints the string by replacing geeks by Geeks print(string.replace("geeks", "Geeks")) # Prints the string by replacing only 3 occurrence of Geeks print(string.replace("geeks", "GeeksforGeeks", 3)) |
Output :
Geeks for Geeks Geeks Geeks Geeks GeeksforGeeks for GeeksforGeeks GeeksforGeeks geeks geeks
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- replace() in Python to replace a substring
- Python | Pandas Series.str.replace() to replace text in a series
- Python String Methods | Set 3 (strip, lstrip, rstrip, min, max, maketrans, translate, replace & expandtabs())
- Python | Replace rear word in String
- Python - Kth word replace in String
- Python - Replace Substrings from String List
- Python | Multiple indices Replace in String
- Python - Replace duplicate Occurrence in String
- Python - Replace all occurrences of a substring in a string
- Python - Replace String by Kth Dictionary value
- Python - Replace to K at ith Index in String
- Python - Replace Different characters in String at Once
- Python - Replace all numbers by K in given String
- Python - Retain first N Elements of a String and Replace the Remaining by K
- Python - Replace vowels in a string with a specific character K
- Replace missing white spaces in a string with the least frequent character using Pandas
- Map function and Lambda expression in Python to replace characters
- Python | Pandas DataFrame.fillna() to replace Null values in dataframe
- Python | Pandas dataframe.replace()
- Python | Pandas Timestamp.replace
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : nidhi_biet

Formed in 2009, the Archive Team (not to be confused with the archive.org Archive-It Team) is a rogue archivist collective dedicated to saving copies of rapidly dying or deleted websites for the sake of history and digital heritage. The group is 100% composed of volunteers and interested parties, and has expanded into a large amount of related projects for saving online and digital history.
