Home » Remove Multiple Characters from a String in Python

Remove Multiple Characters from a String in Python

by Online Tutorials Library

Remove Multiple Characters from a String in Python

We already know that strings are defined as a sequence of characters, and we can perform a variety of operations on them.

In this tutorial, we will learn one more interesting task that can be accomplished using strings in Python.

Here we will see how we can remove multiple characters from it.

We have listed below the methods that we will learn to meet our objective.

  1. Using nested replace()
  2. Using translate() & maketrans()
  3. Using subn()
  4. Using sub()

Using nested replace()

In the program given below, we will see how replace() is used to remove multiple characters from the string.

Output:

The initialized string is  learnpythonlearn  The string after replacing the characters is  eearnpythoneearn  

Explanation-

  1. In the first step, we have initialized the string whose characters we would like to replace.
  2. After this, we have displayed the original string so that we can easily understand the difference between this and the expected output.
  3. Now we have used replace() and specified the characters that we wish to remove or change.
  4. On executing the program, the desired output is displayed.

In the second program, we will see how translate() and maketrans() can be used to do the same. The users must keep this thing in their mind that it only works in Python 2.

Using translate() and maketrans()

The following program shows how it can be done.

Output:

The initialized string is  learnpythonlearn  The string after replacing the characters is  eearnpythoneearn  

Explanation-

  1. In the first step, we have initialized the string whose characters we would like to replace.
  2. After this, we have displayed the original string so that we can easily understand the difference between this and the expected output.
  3. Now we have used replace() and specified the characters that we wish to remove or change.
  4. On executing the program, the desired output is displayed.

Now we will discuss how re.subn() can become an aid for this. The subn() returns a new string with the total number of replacements.

Using re.subn()

The program given below shows how it can be done.

Output:

To get the result AAA, we can multiply AA by AA  

Explanation-

  1. In the first step, we have imported the re module that will help us to use the required functions.
  2. After this, we have initialized the string whose characters we would like to replace or remove.
  3. The next step is to define a function that takes the string value as its parameter.
  4. In the function definition, we have used subn() that takes three parameters. The first parameter is the pattern that we wish to replace, the second is with what element or number we want to replace it and the third is the string.
  5. Finally, the print statement at the end shows that the processed string is displayed.
  6. We have passed this string at the end so that we get the expected output.

In the last program, we will do the same thing using sub()

Using re.sub()

The following program illustrates how it can be done-

Output:

To get the result ZZZ, we can multiply ZZ by ZZ  

Explanation-

  1. In the first step, we have imported the re module that will help us to use the required functions.
  2. After this, we have initialized the string whose characters we would like to replace or remove.
  3. The next step is to define a function that takes the string value as its parameter.
  4. In the function definition, we have used sub() that takes three parameters. The first parameter is the pattern that we wish to replace, the second is with what element or number we want to replace it and the third is the string.
  5. Finally, the print statement at the end shows that the processed string is displayed.
  6. We have passed this string at the end so that we get the expected output.

Conclusion

In this tutorial, we learned how we can remove multiple characters from a string using Python.


Next TopicShuffle in Python

You may also like