Quantcast
Channel: Go4Expert
Viewing all articles
Browse latest Browse all 1987

how can I remove punctuation from a string in python

$
0
0
For Example

s = "AAA? BBB. CCC!"

So, I do:

import string
table = str.maketrans('', '', string.punctuation)
s = [w.translate(table) for w in s]

And it's all right. My new sentence will be:

s = "AAA BBB CCC"

But, if I have input sentence like:

s = "AAA? BBB. CCC! DDD.EEE"

after remove punctuation the same method as below I'll have

s = "AAA BBB CCC DDDEEE"

but need:

s = "AAA BBB CCC DDD EEE"

Is any ideas/methods how to solve this problem?

Viewing all articles
Browse latest Browse all 1987

Trending Articles