Home ยป How to Remove Punctuation marks from a String

How to Remove Punctuation marks from a String

Java SE 11 Developer (Upgrade) [1Z0-817]
Java SE 11 Programmer I [1Z0-815] Practice Tests
1 Year Subscription
Java SE 11 Programmer II [1Z0-816] Practice Tests
Spring Framework Basics Video Course
Oracle Java Certification

In this article we show how to remove punctuation marks from a string in python

There are 14 punctuation marks that are used in the English language.

They are: the period, question mark, exclamation point, comma, semicolon, colon, hyphen, dash, braces, brackets, parentheses, apostrophe, quotation mark, and ellipsis.

Example

# Remove Punctuation in a String

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

myString = "This is, a test-string (full) of punctuations ! [ok]?"

newString = ""

for char in myString:
    if char not in punctuations:
        newString = newString + char

print("\nThe Original String")
print(myString)

print("\nThe Final String")
print(newString)

When you run this you will see something like this

>>> %Run removepunctuations.py

The Original String
This is, a test-string (full) of punctuations ! [ok]?

The Final String
This is a teststring full of punctuations  ok

You may also like

Leave a Comment

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More