
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How to Remove Punctuations From a String in Python?
The fastest way to strip all punctuation from a string is to use str.translate(). You can use it as follows −
Example
import string s = "string. With. Punctuation?" print s.translate(None, string.punctuation)
Output
This will give us the output −
string With Punctuation
Example
If you want a more readable solution, you can explicitly iterate over the set and ignore all punctuation in a loop as follows −
s = "string. With. Punctuation?" exclude = set(string.punctuation) s = ''.join(ch for ch in s if ch not in exclude) print s
Output
This will give us the output −
string With Punctuation
- Related Articles
- Removing punctuations from a string using JavaScript
- How to remove specific characters from a string in Python?
- Count total punctuations in a string - JavaScript
- Remove Vowels from a String in Python
- How to remove characters except digits from string in Python?
- How to remove “,” from a string in JavaScript
- How to Remove Characters from a String in Arduino?
- Program to remove duplicate characters from a given string in Python
- Remove all duplicates from a given string in Python
- How to remove all special characters, punctuation and spaces from a string in Python?
- How to remove a particular character from a String.
- How to remove certain characters from a string in C++?
- How to remove html tags from a string in JavaScript?
- Python Pandas – Remove numbers from string in a DataFrame column
- How can I remove the ANSI escape sequences from a string in python?

Advertisements