I want preprocess strings using Python.
Here is an example.
Given a string
string = "hello... (world)!"
desired_string = "hello . . . ( world ) !"
string = string.replace(".", " . ")
string = string.replace("(", " ( ")
string = string.replace(")", " ) ")
string = string.replace("!", " ! ")
>>> string
'hello . . . ( world ) ! '
.split
re.sub
using re
adds white-space before and after the desired characters:
import re
pat = re.compile(r"([.()!])")
print (pat.sub(" \\1 ", string))
# hello . . . ( world ) !