string.punctuation in Python
In Python, the string module is a pre-initialized string used as a string constant. One such constant is string.punctuation, which provides a predefined string containing all the characters commonly considered punctuation.
What is string.punctuation?
string.punctuation
is a string constant containing the following characters:
!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~
This constant comes in handy when we need to identify or remove punctuation from text. It saves us the hassle of manually defining or remembering all punctuation characters.
import string
# Check if a character is punctuation
char = "!"
if char in string.punctuation:
print(f"{char}")
Output
!
Explanation:
- The
string
module is imported to accessstring.punctuation
. - The
if char in string.punctuation
statement checks whether the value ofchar
("!"
) exists in thestring.punctuation
string. If it does, the condition evaluates toTrue
, and the corresponding message is printed.
Table of Content
Syntax of string.punctuation
string.punctuation
Parameters:
- None. This is a predefined constant in the
string
module.
Return Type:
- Type:
str
(Returns a string containing all standard punctuation characters).
Examples of string.punctuation
1. Removing Punctuation from a String
If we have a text string and want to strip out punctuation, string.punctuation is used to
make this simple:
import string
# Input string
s = "Learn Python, with GFG!!!"
# Remove punctuation
clean_txt = ''.join(ch for ch in s if ch not in string.punctuation)
print(clean_txt)
Output
Learn Python with GFG
Explanation:
- The code uses a generator expression to iterate through each character in the input string
's'
. It includes only those characters that are not present instring.punctuation
. join()
method combines the filtered characters into a new string, effectively removing all punctuation from the input string.
2. Counting Punctuation in a String
We can also count how many punctuation marks appear in a string:
import string
# Input string
s = "Wow! Amazing... Isn't it?"
# Count punctuation marks
count = sum(1 for ch in s if ch in string.punctuation)
print(count)
Output
6
Explanation:
- The code iterates over each character in the input string
's'
using a generator expression. For each character, it checks if the character is instring.punctuation
. - The
sum()
function accumulates1
for every character in's'
that is found instring.punctuation
, effectively counting the total number of punctuation marks in the string.
3. Filtering Punctuation from a List
When working with lists, we might want to filter out punctuation:
import string
# List of characters
c = ['a', ',', 'b', '!', '?']
# Filter punctuation
non_punc = [ch for ch in c if ch not in string.punctuation]
print(non_punc)
Output
['a', 'b']
Explanation:
- The list comprehension iterates through each character in
the
list and excludes any character found instring.punctuation
. - The filtered characters (
'a'
and'b'
) that are not punctuation marks are stored in a new list,non_punc
.