Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is the Python String Replace() Method?
3.
Syntax of Python String Replace() method
4.
Parameters of Python String Replace() Method
5.
Python String Replace() Return Value
6.
How does the Python String Replace() method work?
7.
Examples of Python String replace() Methods
7.1.
Example 1
7.1.1.
Output
7.2.
Example 2 
7.2.1.
Output 
7.3.
Example 3
7.3.1.
Output
8.
Frequently Asked Questions
8.1.
How do you replace a string in Python?
8.2.
Can I use replace with strings in Python?
8.3.
How do you replace a sub string in a string in Python?
8.4.
How do you replace values in Python?
9.
Conclusion
Last Updated: Mar 27, 2024
Easy

Python String replace() Method

Author Teesha Goyal
0 upvote
gp-icon
Basics of Python
Free guided path
7 chapters
99+ problems
gp-badge
Earn badges and level up

Introduction

Python is an open-source, high-level, object-oriented programming language. It has a vast collection of built-in modules and libraries which can be imported into your python program. It makes building complex programs easier. 

This article will discuss the Python String replace() Function. The python string replace() function is a standard function; hence there is no need to import it. 

python string replace

What is the Python String Replace() Method?

What if there is a case where you need to replace something in the string with something new, we can implement it, which can be hectic to write the code from scratch, but Python itself has some features to achieve this. The Python string Replace function is an in-built function that replaces a substring from an existing string with a new substring. 

In the process of replacing, we need the old substring and the new substring by which we can replace the old one with the new one. (optional) And one more thing you will have to take care of is the count of the old substring. So It takes three arguments - old, new, and count are explained in one of the next sections of this article. 

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Syntax of Python String Replace() method

The syntax of the Python string Replace function is -

string.replace(old, new, count)

Parameters of Python String Replace() Method

  • old: The existing or old substring you want to replace.
  • new: The new substring you want to replace the old one with.
  • count: (optional) It decides how many instances of the old substring are to be replaced with the new substring. If not passed, all the instances of the old substring are replaced with a new substring.
Python string replace function

Python String Replace() Return Value

The return value of the function replace() is the result of the string after replacing the substring with the new substring. Here's an example for understanding the return value of replace() function in Python.

''' Here 'a' is the original string with value "Coding Studio" '''
a = "Coding Studio"

''' Printing 'a' before replacing '''
print(a)

''' Here 'res' is the return value after replacing "Coding" with "Code" in 'a' '''
res = a.replace("Coding", "Code")

''' Printing value of result 'res' after replacing '''
print(res)

 

In the above example, ‘a’ is the original string with the value “Coding Studio”. Then replace() function is called to replace the “Coding” in 'a' with “Code” which returns a new string named ‘res’. The output of the above example:

Coding Studio
Code Studio

How does the Python String Replace() method work?

The working of the Python String Replace() method is as follows:

original_string: This is the string where the replacements are to be made.

old_substring: This is the substring within the original_string that you wish to replace.

count: (Optional) The number of times the new substring should be substituted for the old substring. 

new_substring: You wish to replace old_substring with this substring.

The replace() method looks for every instance of the old_substring in the original_string and replaces it with the new_substring. The original text remains unchanged while a new string containing these substitutions is returned.
 

Examples of Python String replace() Methods

Example 1

'''Driver Function'''
def main():
    
    string = "We are Ninjas! We work like Ninjas!"
    
    ''' replacing 1 instance of Ninjas with Coders.'''
    print(string.replace("Ninjas", "Coders", 1))
    
    '''replacing all instances of Ninjas with Coders.'''
    print(string.replace("Ninjas", "Coders"))
    
'''Executing Main'''
main()

Output

We are Coders! We work like Ninjas!
We are Coders! We work like Coders!

 

The python string replace() function does not change the existing string. It creates and returns a new string with old substrings replaced with new substrings. If you want to use the new string later in the program, then you must save the new string with a new variable. 

Example 2 

'''Driver Function'''
def main():
    
    string = "We are Ninjas! We work like Ninjas!"
    
    ''' replacing and printing the strings.'''
    print(string.replace("Ninjas", "Coders", 1))
    
    '''Printing the original string'''
    print(string)
    
    '''Saving the value in new variable'''
    new_string = string.replace("Ninjas", "Coders")
    
    '''Printing the new_string'''
    print(new_string)
    
'''Executing Main'''
main()

Output 

We are Coders! We work like Ninjas!
We are Ninjas! We work like Ninjas!
We are Coders! We work like Coders!


The python string replace function is case-sensitive. The old substring is not replaced if you pass it in a different case. 

Example 3

'''Driver Function'''
def main():
    
    string = "We are Ninjas! We work like ninjas!"
    
    '''Passing the new string with different case.'''
    print(string.replace("ninjas", "Coders"))
    
'''Executing Main'''
main()

Output

We are Ninjas! We work like Coders!

 

You can practice by yourself with the help of online python compiler for better understanding.

Frequently Asked Questions

How do you replace a string in Python?

The replace() is the function in Python that we can use to replace the original/old string with the new string. This function returns the result string after replacing the new string with the old string.

Can I use replace with strings in Python?

Yes, you can replace use replace with strings in Python. The replace() function accepts the string object that replaces the old string with the new string. We can use this function to replace the complete string and substring in Python.

How do you replace a sub string in a string in Python?

The function replace() can be used to replace the substring in a string in Python, where in parameters, we just have to pass the old substring and the new substring; this will return a new string with replaced substring.

How do you replace values in Python?

The replace() method produces a duplicate of the string that replaces the previous substring with the new one.  The map() function and a lambda function can also be used to replace values in a list.

Conclusion

This article discussed the Python String Replace() function. We saw that it is a case-sensitive function, and it does not change the original string.  

I hope you would have gained a better understanding of these topics now!

Recommended problems -

Happy Coding!

Previous article
What is String Slicing in Python?
Next article
Python String Concatenation
Guided path
Free
gridgp-icon
Basics of Python
7 chapters
127+ Problems
gp-badge
Earn badges and level up
Live masterclass