string.punctuation in Python

In Python3, string.punctuation is a pre-initialized string used as string constant. In Python, string.punctuation will give the all sets of punctuation.

Syntax : string.punctuation

Parameters : Doesn’t take any parameter, since it’s not a function.



Returns : Return all sets of punctuation.

Note : Make sure to import string library function inorder to use string.punctuation

Code #1 :

filter_none

edit
close

play_arrow

link
brightness_4
code

# import string library function 
import string 
    
# Storing the sets of punctuation in variable result 
result = string.punctuation 
    
# Printing the punctuation values 
print(result) 

chevron_right


Output :

!"#$%&'()*+, -./:;<=>?@[\]^_`{|}~

 
Code #2 : Given code tests for punctuation.

filter_none

edit
close

play_arrow

link
brightness_4
code

# import string library function 
import string 
    
# An input string.
Sentence = "Hey, Geeks !, How are you?"
  
for i in Sentence:
      
    # checking whether the char is punctuation.
    if i in string.punctuation:
          
        # Printing the punctuation values 
        print("Punctuation: " + i)
   

chevron_right


Output:

Punctuation:,
Punctuation: !
Punctuation:,
Punctuation: ?


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : shubham_singh



Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.