Open In App

string.punctuation in Python

Last Updated : 24 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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.

Python
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 access string.punctuation.
  • The if char in string.punctuation statement checks whether the value of char ("!") exists in the string.punctuation string. If it does, the condition evaluates to True, and the corresponding message is printed.

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:

Python
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 in string.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:

Python
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 in string.punctuation.
  • The sum() function accumulates 1 for every character in 's' that is found in string.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:

Python
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 in string.punctuation.
  • The filtered characters ('a' and 'b') that are not punctuation marks are stored in a new list, non_punc.


Next Article
Practice Tags :

Similar Reads