w3resource

Python Exercises: Remove punctuations from a string

Python String: Exercise-102 with Solution

From Wikipedia,
Punctuation is the use of spacing, conventional signs, and certain typographical devices as aids to the understanding and correct reading of written text. The marks, such as full stop, comma, and brackets, used in writing to separate sentences and their elements and to clarify meaning.

Write a Python program to remove punctuations from a given string.

Sample Solution-1:

Python Code:

import string
def remove_punctuations(text):
    for c in string.punctuation:
        text = text.replace(c,"")
    return text

text = "String! With. Punctuation?"
print("Original text:")
print(text)
result = remove_punctuations(text)
print("After removing Punctuations from the said string:")
print(result)

Sample Output:

Original text:
String! With. Punctuation?
After removing Punctuations from the said string:
String With Punctuation

Flowchart:

Flowchart: Add two strings as they are numbers (Positive integer values).

Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:


Sample Solution-2:

Python Code:

import string
def remove_punctuations(text):
    return text.translate(str.maketrans('', '', string.punctuation))
text = "String! With. Punctuation?"
print("Original text:")
print(text)
result = remove_punctuations(text)
print("After removing Punctuations from the said string:")
print(result)

Sample Output:

Original text:
String! With. Punctuation?
After removing Punctuations from the said string:
String With Punctuation

Flowchart:

Flowchart: Add two strings as they are numbers (Positive integer values).

Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:


Sample Solution-3:

Python Code:

def remove_punctuations(text):
    punc_list = '''!()-[]{};:'"\,<>./[email protected]#$%^&*_~'''
    result = ""
    for char in text:
        if char not in punc_list:
            result = result + char
    return result
text = "@^&$String! With.-- Punctuation?"
print("Original text:")
print(text)
result = remove_punctuations(text)
print("\nAfter removing Punctuations from the said string:")
print(result)

Sample Output:

Original text:
@^&$String! With.-- Punctuation?

After removing Punctuations from the said string:
String With Punctuation

Flowchart:

Flowchart: Add two strings as they are numbers (Positive integer values).

Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:


Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous Python Exercise: Add two strings as they are numbers (Positive integer values).
Next Python Exercise: Replace a word with hash characters in a string.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Share this Tutorial / Exercise on : Facebook and Twitter

Python: Tips of the Day

Getting sorted iterable (can sort by "compare" function):

>>> a = [1, 2, -3]
>>> sorted(a)
[-3, 1, 2]

>>> sorted(a,key=abs)
[1, 2, -3]