Python Program to Remove Punctuations from a String

Write a Python program to remove punctuations from a string. We declared a string of possible punctuation characters and used for loop to iterate the given text. In this python example, the if statement checks each character against punctuations, and if it is not found, char will assign to a new string, which removes the punctuations.

# Python Program to Remove Punctuations in a String

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

orgStr = "Hi!!, Welcome, Tutorial-Gateway?"

newStr = ""

for char in orgStr:
    if char not in punctuations:
        newStr = newStr + char

print("\nThe Original String Before Removing Punctuations")
print(orgStr)

print("\nThe Final String After Removing Punctuations")
print(newStr)
Python Program to Remove Punctuations from a String

This Python program allows the user to enter a string and removes the punctuations from it.

orgStr = input("Please Enter Any String = ")

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

newStr = ""

for char in orgStr:
    if char not in punctuations:
        newStr = newStr + char

print("\nBefore Removing")
print(orgStr)

print("\nAfter Removing")
print(newStr)
Please Enter Any String = Learn@#&^%$# Python<>? Programs?

Before Removing
Learn@#&^%$# Python<>? Programs?

After Removing
Learn Python Programs

Python program to remove punctuations from a string using a while loop.

orgStr = input("Please Enter Any Text = ")

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

newStr = ""

i = 0
while i < len(orgStr):
    if orgStr[i] not in punctuations:
        newStr = newStr + orgStr[i]
    i = i + 1

print("\nBefore Removing Punctuations")
print(orgStr)

print("\nTAfter Removing Punctuations")
print(newStr)
Please Enter Text = hi!!@ tutorial @@#&^ gateway {}\ followers

Before Removing Punctuations
hi!!@ tutorial @@#&^ gateway {}\ followers

After Removing Punctuations
hi tutorial  gateway  followers