In python, the string replace function, replace()
, is used to replace the occurrences of a substring with a different substring. It is an inbuilt string function that returns a copy of the string where occurrences of the original substring have been replaced by the new substring.
s = "to be or not to be" print(s.replace("be", "code"))
Output:
to code or not to code
Table of Contents
- Syntax
- Examples
Syntax
The following is the syntax to use the python string replace function, replace()
:
str.replace(old, new, count)
Parameters:
- old: The old substring to be replaced.
- new: The new substring you want to replace the old substring with.
- count (optional): The number of times you want to replace the occurrence of the old substring with the new one. If it’s not provided, all the occurrences of the old substring are replaced by the new substring.
Returns:
A copy of the original string with occurrences of the old substring replaced by the new substring.
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
Note: If the value passed for the old
parameter is not found, a copy of the original string without any change is returned.
Examples
Example 1: Replace all the occurrences of underscores _
with a single space ' '
.
s = "to_be_or_not_to_be" print("Original string: ", s) # replace _ with a ' ' replaced_s = s.replace("_", " ") print("Replaced sring: ", replaced_s)
Output:
Original string: to_be_or_not_to_be Replaced String: to be or not to be
In the above example, all the occurrences of the underscore symbol, _
, have been replaced by a single space, ' '
, in the returned string.
Example 2: When the substring to be replaced is not present
s = "to be or not to be" print("Original string: ", s) replaced_s = s.replace("was", "code") print("Replaced string: ", replaced_s)
Output:
Original string: to be or not to be Replaced string: to be or not to be
In the above example, since the value passed for the old
parameter, was
is not present in the string, a copy of the original string without any replacement is returned.
Example 3: Replacing only a fixed number of occurrences.
s = "to be or not to be" print("Original string: ", s) replaced_s = s.replace("be", "code", 1) print("Replaced string: ", replaced_s)
Output:
Original string: to be or not to be Replaced string: to code or not to be
In the above example, we pass the third argument to the replace function. This argument controls the number of replacements to be made. Since we passed 1
, only the first occurrence (from left) of the old substring be
was replaced by the new substring code
and all the other occurrences were unchanged in the returned string.
For more on python string replace function, refer to the python docs.
Tutorials on python strings:
- Python – Trim Whitespace Characters from Strings
- Python – Reverse a String with Examples
- Python – Remove Character From String
- Python – Frequency of each word in String
- Python – Convert List to a String
- Python String Count – With Examples
- New String Functions in Python 3.9
- Python String Find – With Examples
- Python String Startswith – With Examples
- Python String Endswith – With Examples
- Python String Split – With Examples
- Python String Join – With Examples
- Python String Concatenation – With Examples
- Python String Replace – With Examples
- Python String Uppercase – With Examples
- Python String Lowercase – With Examples
- Python String Strip – With Examples
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.