how to find and replace all the punctuation in python strings

Solutions on MaxInterview for how to find and replace all the punctuation in python strings by the best coders in the world

showing results for - "how to find and replace all the punctuation in python strings"
Marcelle
12 Jan 2019
1#import the regex library (pip install it with -> "pip install regex"
2import re
3
4test_phrase = 'This is a string! Bust it has punctuation. How can we remove it?'
5
6#We're going to replace the punctution with a whitespace
7clean = ' '.join(re.findall('[^!.?]+', test_phrase))
8#         						^ Place the punctuation that you want
9#								  to remove in the square brackets.
10print(clean)
11> 'This is a string But it has punctuation How can we remove it'