Python string.punctuation | Get all sets of punctuation

In this tutorial, we are going to learn about punctuation in Python, how to display all set of punctuation, how to remove it.

These topics are small parts of very huge subjects, so let’s learn it with some cool examples and basics of coding.

What is Punctuation?

A string contains letters, whitespace, numbers and it has punctuation, these characters include commas and period of semicolons.

Display all set of punctuations in Python:

We can display all the set of punctuation marks using just an inbuilt function as string.punctuation in Python.

import string
for x in string.punctuation:
    print("[" + x + "]")



These are the sets of punctuation in Python string.punctuation library.

Removing all punctuation from a string:

Example: let’s solve it with our previous knowledge first. Removing all the punctuation marks:-

str  = input("Enter")
punctuation = '''[email protected]#$%^&*(){}[]|._-`/?:;"'\,~''';
str1 = ""
i = 0
while (i<len(str)):
    if str[i] not in punctuation:
        str1 = str1+str[i]
    i = i+1


#[email protected] W!E%L%C(O){M}#E# ^Y&[email protected]'



Here we created a dictionary for all the set of punctuation marks first, initialize a variable I from 0 for a while loop and if there aren’t any punctuation marks in the string, it will add to str1. after completing the while loop it will print the str1.

Example: Take Input from the user and remove all the set of punctuation from it and place a space on behalf of punctuation marks.

a = input("Enter the string")
i = 0
punctuation = '''[email protected]#$%^&*(){}[]|._-`/?:;"'\,~''';
while i<len(a.lower()):
    if a[i] in punctuation:
        a = a.replace(a[i]," ");


#[email protected] W!E%L%C(O){M}#E# ^Y&[email protected]'


 W E W E L C O  M  E   Y O U

Example 2:

You have given a String as an input, replace it with punctuation, import the string library and solve this using a function .

import string
def str_punctuation(value):
    result = ""
    for x in value:
        if x not in string.punctuation:
            result += x
    return result

Input = "[email protected] W!E#L#COME% YOU"




With the “in” operator and the string.punctuation constant, we can remove all punctuation chars from a string.

The idea to encode or decode some private messages we can all use the method of removing the punctuation or some numeric codes. Lets solve an example of it. Like we have to decode a message:

a = input("Enter the message you have  to decode:")
i = 0
punctuation = '''[email protected]#$%^&*(){}[]|._-`/?:;"'\,~12345678876543''';
while i<len(a.lower()):
    if a[i] in punctuation:
        a = a.replace(a[i]," ");
print("The decoded message is:")


Enter the message you have  to decode: #%@#[email protected]#T#$H#@E$R#@E$%&I$%S$#%[email protected]@#[email protected]$I#[email protected]%@%U$%R$%@@C%^O$#D$%E


The decoded message is:
T  H  E R  E   I  S   B  U  G   I N     U  R    C  O  D  E

In this article, we learned about the punctuation marks in Python, how to remove it and also how to use it in our various daily life uses.

If you have any queries please comment below.

Also, read: Python program to count the number of occurrences of a substring in a string


One response to “Python string.punctuation | Get all sets of punctuation”

  1. oluwande adewoyin says:

    how can i add to the list of python punctuation

Leave a Reply

Your email address will not be published. Required fields are marked *