Do you think IIT Guwahati certified course can help you in your career?
No
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.
What is String Replace() Method in Python?
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.
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.
Return Value of String Replace() methon in Python
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.
Python
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)
You can also try this code with Online Python Compiler
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 String Replace() method work in Python?
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 String replace() Methods in Python
Example 1
Python
Python
'''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()
You can also try this code with Online Python Compiler
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
Python
Python
'''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()
You can also try this code with Online Python Compiler
You can practice by yourself with the help of online python compiler for better understanding.
Frequently Asked Questions
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.
What is replace() in Python?
The replace() method in Python is a string method used to replace a specified substring with another substring within a string. It returns a new string with the replacements applied.
Can the replace() method modify the original string?
No, the replace() method cannot modify the original string, as strings in Python are immutable. Instead, it returns a new string with the replacements, leaving the original string unchanged.
Is the replace() method case-sensitive?
Yes, the replace() method is case-sensitive. It only replaces exact matches of the specified substring, so "text" and "Text" would be considered different.
Can replace() be used on other data types, or is it specific to strings?
The replace() method is specific to strings in Python. While similar methods may exist for other data types, replace() only works directly on string objects.
Conclusion
In this article, we have discussed the Python String replace() Method. The replace() method in Python is a powerful and versatile tool for handling string modifications, allowing users to replace specific substrings seamlessly. Though the method is case-sensitive and does not alter the original string (as Python strings are immutable), it provides an easy way to generate new strings with desired changes.