Python: Strip a set of characters from a string – w3resource

Python: Strip a set of characters from a string

Last update on August 19 2022 21:50:47 (UTC/GMT +8 hours)

Python String: Exercise-41 with Solution

Write a Python program to strip a set of characters from a string.

Python String Exercises: Strip a set of characters from a string

Sample Solution:-

Python Code:

def strip_chars(str, chars):
    return "".join(c for c in str if c not in chars)

print("\nOriginal String: ")
print("The quick brown fox jumps over the lazy dog.")
print("After stripping a,e,i,o,u")      
print(strip_chars("The quick brown fox jumps over the lazy dog.", "aeiou"))
print()

Sample Output:

Original String:                                                                                              
The quick brown fox jumps over the lazy dog.                                                                  
After stripping a,e,i,o,u                                                                                     
Th qck brwn fx jmps vr th lzy dg.                             

Flowchart:

Flowchart: Strip a set of characters from a string

Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to reverse words in a string.
Next: Write apython program to count repeated characters in a string.