Python Strings: Replace sub-strings within strings

Within a Python string if you need to replace a portion with something else, the replace() function would be your go to guy! In Python, strings are considered to be objects and since replace() function is applied on strings it is considered to be a method of the string object.

In this article I will explain how to use replace function for string manipulation.

string.replace() : How to Use

The replace function accepts the following 3 parameters:

a. old_string_portion: The portion of the string object that needs to be replaced
b. new_string_portion: The replacement string
c. Instances: Number of instances to be replaced

To understand better. Let’s consider a simple example.

Consider the following sentence: “Jack and Jill, went up the hill.” Let’s say, we want to replace “Jack” with “John” without changing rest of the sentence. The following code using the replace() function makes it possible:

#store the string to be handled in a variable
string_sample="Jack and Jill, went up the hill!"

#applying replace function
s=string_sample.replace("Jack","John")

#printing the results
print(s)
print(string_sample)

The output is as follows:

John and Jill, went up the hill!
Jack and Jill, went up the hill!

Do note that , the replace function doesn’t change the original string. Instead it creates a new string. In the above example the new string was stored in variable “s”.

Also note that we didn’t pass any value for the instances parameter. By default, all instances of the string are replaced.

In the next code, we will replace the first 2 instances of “and” with “&” in a string:

#store the string to be handled in a variable
string_sample="Jack and Jill, went up the hill! They stayed there and had a great time. On the way back, they met Hansel and Gretel."

#applying replace function
s=string_sample.replace("and","&",2)

#printing the results
print(s)

The code output is as follows:

Jack & Jill, went up the hill! They stayed there & had a great time. On the way back, they met Hansel and Gretel.

Do note that, “Hansel and Gretel” stays the same, while the first 2 instances of “and” got replaced by “&“.

Replacing multiple sub-strings

When you are using replace function, you can replace phrases and not just words. The example below would work perfectly fine:

sample_string.replace("Jack and Jill","Tom and Mary")

However, can we replace multiple portions in the string? For example , can we replace “Jack” with “John” and replace “hill” with “mountain” ? We can’t do that in one step. We would need to apply replace function twice:

#store the string to be handled in a variable
string_sample="Jack and Jill, went up the hill!"

#applying replace function
s=string_sample.replace("Jack","John").replace("hill","mountain")

#printing the results
print(s)

How does the above code work ? The first replace function creates a new string : “John and Jill, went up the hill“. The second replace function is applied on this new string. The replaces “hill” with “mountain“. The final output that we see is as follows:

John and Jill, went up the mountain!

Practical uses of replace function

Here are some practical cases where replace function could be useful:

  1. Creating Custom prospect emails : Create a template. Replace “Name” with the actual name of the prospect.
  2. Cleaning punctuations from a text file
  3. Removing extra spaces from a document

You may want to try out some of these. If you have any questions do write to me or comment on this post.

Further Reading

If you are interested in Python string handling you may want to read on how split function splits a python string into a list of sub-strings.

3 thoughts on “Python Strings: Replace sub-strings within strings”

Leave a Reply

Your email address will not be published. Required fields are marked *