String replacement is a common task in Python. A simple yet powerful way to replace a string in Python is by using the Python string replace() method. In today’s post, we’ll not only explain this method but will understand it from a practical point of view.
Python is one of the best scripting languages that most easily integrates with Selenium Webdriver for web-based automation, which requires a lot of string handling. There you may need to use string handling for building dynamic XPath, comparing dates, and searching for substrings. Hence, it’s inevitable to avoid using the Python string replace() method.
Replace String in Python with Examples
Nowadays, Python is also becoming popular for use in data analytics applications. Here you first need to process the data and then have to represent it in graphs. And, all such operations require string manipulation using methods like the Python string replace(). Next, apart from knowing about these methods, you also have to think creatively about using string functions. So, you can use them more efficiently.
In one of our earlier tutorials, we discussed Python Strings at great length. Perhaps you should read this post if you wish to learn strings from depth. However, to evaluate yourself for how logically you can use strings, please try this Python string quiz.
How to replace a string in Python
Often you’ll get to deal with a string (str object)
in Python to modify its contents by replacing a part of the text with another.
In Python, everything is an object which is also true for strings. It means that str
is a string object. And, Python’s string module provides a replace() method. We can call it in one of the following ways.
1- Calling replace() with the str
object prefixing with a dot.
2- Call it directly after importing the string module.
Python replace() method signature
The <string.replace()> method has the following syntax.
str.replace(s, old, new[, replacefreq])
Here is a summary of the parameters passed to the method.
Parameters | Description |
---|---|
<s> | It’s the value of a string to search and replace from. |
<old> | It’s the value of the old sub-string you like to replace. |
<new> | It’s the value of the new substring that you want to replace with the old one. |
<replacefreq> | It represents the number of times the string will get replaced. |
Examples
We have added two examples to showcase how to replace a string in Python. Copy the code from these into your IDE, have a deeper look, and run.
Example 1: Using the module name to call replace()
oldString = 'I love Python 2.0' import string newString = string.replace(oldString, '2.0', '3.0') print(newString) newString = string.replace(oldString, '2.0', '3.0.') print(newString) oldString = 'Are you a tester who tests websites? Be a good tester.' newString = string.replace(oldString, 'test', 'develop', 1) print(newString) newString = string.replace(oldString, 'test', 'develop', 2) print(newString) newString = string.replace(oldString, 'test', 'develop', 3) print(newString)
The above Python code will produce the following output.
I love Python 3.0 I love Python 3.0. Are you a developer who tests websites? Be a good tester. Are you a developer who develops websites? Be a good tester. Are you a developer who develops websites? Be a good developer.
Example 2: Using the <str> object to call replace()
oldString = 'I love Python 2.0' newString = oldString.replace('2.0', '3.0') print(newString) newString = oldString.replace('2.0', '3.0.') print(newString) oldString = 'Are you a tester who tests websites? Be a good tester.' newString = oldString.replace('test', 'develop', 1) print(newString) newString = oldString.replace('test', 'develop', 2) print(newString) newString = oldString.replace('test', 'develop', 3) print(newString)
Here is the execution report:
I love Python 3.0 I love Python 3.0. Are you a developer who tests websites? Be a good tester. Are you a developer who develops websites? Be a good tester. Are you a developer who develops websites? Be a good developer.
From both of the above examples, it’s evident that you can use any of two methods to replace strings. Though, it’s the second method (in example 2) that is more convenient to use.
Must read – How to replace chars/substrings in Python string
Hopefully, the above post will have given you a fair idea of using the string replace() method in Python. You can learn more through practice, get the code from the examples, and start.
If you want us to cover any other topic in Python, then please write to us.
All the Best,
TechBeamers