How to use the Python Replace method – with examples

  • Reading time:15 mins read

What is the Python Replace method?

The Python replace method is a built-in method that is used to return a copy of a substring after some parts of the string have been removed.

 

What is the Python Replace method used for?

Since the method is built-it that means that we can call it anywhere within our code. In its simplest form, the Python replace method takes a character from a string and replaces it with a different character that has been specified.

 

python replace

 

Example 1

The replace method can also be used to clean up data by replacing bad or unwanted characters in a string. For illustration purposes, we will create a variable that stores a string with some characters that we want to remove.

 

 

The string we have just created contains three instances of unwanted characters. For example, we have a new line escape character that splits the string into two as shown in the output and the number 4 in place of the character ‘a’.

We are going to create a variable that is going to store the new string with the unwanted characters removed. Once we have the variable declared we now need to call the Python replace method.

We will first begin by stating the name of the string that we want to do the replacement and then append the Python replace method using the dot operator.

In this case, the string that we want to replace characters in is called ‘my_string’ after which we will append the Python replace method using the dot operator followed by a pair of parentheses.

 

python replace

 

Example 2

Now inside the parentheses, we are going to specify two arguments: the character that we want to be replaced inside a pair of quotes followed by the replacement character, these two should be separated by a comma.

Using the string that we just created we want to replace the newline character with nothing. Therefore the replacement character will be quotation marks with no character specified in between as shown below.

 

 

As shown in the output above we have successfully removed the newline character and we now have a single line string. This method is particularly useful when dealing with data from an outside source that may need to be tidied up before it can be used.

 

python replace

 

Example 3

We also want to replace all instances of the numbers with letters. Now using the same procedure all that we need to do is chain another replace method using the dot operator next to the one that we just used to remove the newline character.

 Now in the new replace method we need to specify the numbers that we want to be removed as the first parameter which in this case is only four and the replacement character which is the letter  ‘a’.

 

 

Using the Python replace method we have successfully removed the unwanted character from the string that we initially had.

However, as you may have already noted, pasting the replace every other time we want to remove a character is a bit hectic and repetitive.

Example 4

A more decent way of removing characters using the replace method is by creating a function that we can call anywhere in our code.

We’re going to call this function clean, and we are going to give it a text value and it is going to return the text after performing the replacement for the unwanted characters.

In the above code once we call the function clean on the string ‘mystring’, the function applies the replace method on our string replacing all unwanted characters. This is a more efficient way of using the replace method.

 

Summary

If you’d like to see more programming tutorials, check out our Youtube channel, where we have plenty of Python video tutorials in English.

In our Python Programming Tutorials series, you’ll find useful materials which will help you improve your programming skills and speed up the learning process.

Programming tutorials

Would you like to learn how to code, online? Come and try our first 25 lessons for free at the CodeBerry Programming School.

Learn to code and change your career!

100% ONLINE

IDEAL FOR BEGINNERS

SUPPORTIVE COMMUNITY

SELF-PACED LEARNING

Not sure if programming is for you? With CodeBerry you’ll like it.