Python Program to Remove Punctuation from String
Use
''.join(char for char in text if char.isalnum() or char.isspace()) to remove punctuation from a string in Python.Examples
InputHello, world!
OutputHello world
InputPython's syntax: simple, yet powerful.
OutputPythons syntax simple yet powerful
InputNo punctuation here
OutputNo punctuation here
How to Think About It
To remove punctuation, check each character in the string and keep it only if it is a letter, number, or space. Skip all punctuation marks by filtering them out.
Algorithm
1
Get the input string.2
Create a new empty string to store result.3
For each character in the input string, check if it is a letter, number, or space.4
If yes, add it to the new string; if not, skip it.5
Return or print the new string without punctuation.Code
python
text = "Hello, world!" clean_text = ''.join(char for char in text if char.isalnum() or char.isspace()) print(clean_text)
Output
Hello world
Dry Run
Let's trace the input 'Hello, world!' through the code.
1
Input string
text = 'Hello, world!'
2
Check each character
H (keep), e (keep), l (keep), l (keep), o (keep), , (skip), (keep), w (keep), o (keep), r (keep), l (keep), d (keep), ! (skip)
3
Build new string
clean_text = 'Hello world'
4
Print result
Output: Hello world
| Character | Keep? |
|---|---|
| H | Yes |
| e | Yes |
| l | Yes |
| l | Yes |
| o | Yes |
| , | No |
| Yes | |
| w | Yes |
| o | Yes |
| r | Yes |
| l | Yes |
| d | Yes |
| ! | No |
Why This Works
Step 1: Filter characters
The code uses char.isalnum() to keep letters and numbers, and char.isspace() to keep spaces, removing punctuation.
Step 2: Join characters
It joins all kept characters into a new string without punctuation using ''.join().
Alternative Approaches
Using string.punctuation and str.translate
python
import string text = "Hello, world!" translator = str.maketrans('', '', string.punctuation) clean_text = text.translate(translator) print(clean_text)
This method is efficient and uses built-in translation table to remove all punctuation characters.
Using regular expressions
python
import re text = "Hello, world!" clean_text = re.sub(r'[^\w\s]', '', text) print(clean_text)
This uses regex to remove all characters except word characters and whitespace.
Complexity: O(n) time, O(n) space
Time Complexity
The program checks each character once, so time grows linearly with string length.
Space Complexity
A new string is created to store the result, so space also grows linearly with input size.
Which Approach is Fastest?
Using str.translate with string.punctuation is generally faster than character-by-character filtering or regex.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Character filtering with isalnum/isspace | O(n) | O(n) | Simple and readable code |
| str.translate with string.punctuation | O(n) | O(n) | Fastest and efficient for large strings |
| Regular expressions | O(n) | O(n) | Flexible pattern matching, but slightly slower |
Use
str.translate with string.punctuation for fast punctuation removal.Forgetting to keep spaces causes words to join together without separation.