Strip Multiple Characters in Python

Sidrah Abdullah Jan 30, 2023 Dec 13, 2021
  1. Use str.replace() to Strip Multiple Characters in Python String
  2. Use the re.sub() Function to Strip Multiple Characters in Python String
  3. Use the string.translate() Method to Strip Multiple Characters in Python String
Strip Multiple Characters in Python

Stripping characters in a string object is an important task you must know as a programmer. Stripping means that you should be able to remove characters from any position.

Before starting with stripping, you must keep in mind that Python string objects are immutable. Therefore, they cannot be modified. Hence, we will store our stripped string in a new string in each example.

In this tutorial, we will look at several methods and examples of striping multiple characters from any position in Python.

Use str.replace() to Strip Multiple Characters in Python String

The str.replace() method provides the stripping functionality. Let’s look at the code and its output first:

original_string = "!(Hell0o)"
characters_to_remove = "!()0"
new_string = original_string

for character in characters_to_remove:
    new_string = new_string.replace(character, "")
print(new_string)

Output:

Hello

What you will do is create a string of characters you want to remove from the string.

Now, assign the original string to a new string to keep the original string intact.

Next, you will use a for loop to literature through the source string. Inside the loop, you will check for the characters you want to remove and replace them with no space using the str.replace() method. Then, you will get the desired output.

The syntax for this function is below.

string.replace(old character, new character, count)

The string is the string from which you want to strip the characters.

The first argument, old character, represents that character you want to replace.

The new character argument implies the character you are looking to replace with.

The third argument, count, is an optional parameter that indicates the number of occurrences of that character you wish to replace. If you pass 2, the function will replace two occurrences of that character.

Use the re.sub() Function to Strip Multiple Characters in Python String

This is another common way to strip the characters from strings in Python. This function is a part of the re module, which consists of regular expressions or regex operations that you can utilize.

For this function, let’s consider a different example:

import re

original_string = "!(pyth000on)"
characters_to_remove = "!()0"
pattern = "[" + characters_to_remove + "]"
new_string = re.sub(pattern, "", original_string)
print(new_string)

Output:

python

Here, we have defined a string consisting of characters we want to remove from our source string. Then we have used the re.sub() function that takes three arguments: regular expression pattern, the string you want to replace it with, and the source string.

From the output, we can see that it has removed the characters. We can also experiment with a longer sentence. For example.

import re

original_string = "!(pyth000on is mYy pro0gra@mm!ing langu@age3)"
characters_to_remove = "!()0Y@!3"
pattern = "[" + characters_to_remove + "]"
new_string = re.sub(pattern, "", original_string)
print(new_string)

Output:

python is my programming language

Use the string.translate() Method to Strip Multiple Characters in Python String

You can also use the string.translate() method to remove the particular number of characters from a Python string. The syntax for this function is below.

string.translate(table)

This function takes an argument, table. This table could be a dictionary or a mapping table describing the characters you want to replace. You can assign empty values to both x and y to strip the characters.

For example:

s = "Python Programming"
x = "P"
y = "B"
table = s.maketrans(x, y)
print(s.translate(table))

Output:

BythonBrogramming

In conclusion, there are three common ways to strip characters from a string object in Python. Out of these three, using the replace() method to strip is much easier than others because it is easier to comprehend and use.

Related Article - Python String