Skip to content
Advertisement

How to automatically remove space before punctuation

For example: “This is some text . This is some text”

should be

“This is some text. This is some text”

We can use replase but replacing ‘ .’ with ‘.’, but it’s not a good approach. Please let me know if you have any other idea which is generalised for any punctuation.

Advertisement

Answer

Sorry I’m on mobile but it would be something like this:

import re
print(re.sub(r's(?=[.,:;])', "", yourstring))
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement