python pad punctuation with spaces

Solutions on MaxInterview for python pad punctuation with spaces by the best coders in the world

showing results for - "python pad punctuation with spaces"
Salomé
22 Mar 2016
1# Basic syntax using regex:
2import re
3re.sub('characters_to_pad', r' \1 ', string_of_characters)
4# Where you can specify which punctuation characters you want to pad in
5#	characters_to_pad
6
7# Example usage:
8import re
9your_string = 'bla. bla? bla.bla! bla...' # An intelligent sentence
10your_string = re.sub('([.,!()])', r' \1 ', your_string)
11# your_string is updated in place
12print(your_string)
13--> bla .  bla? bla . bla !  bla .  .  . 
14# Notice that the '?' didn't have spaces added because it wasn't listed
15#	among the characters_to_pad in the above function