Different methods can be used for removing leading and trailing spaces, for converting multiple spaces to one and to remove spaces before exclamation mark, comma etc:
mystr = " Hello . To , the world ! "
print(mystr)
mystr = mystr.strip() # remove leading and trailing spaces
import re # regex module
mystr = re.sub(r'\s+'," ", mystr) # convert multiple spaces to one space.
mystr = re.sub(r'\s*([,.!])',"\\1", mystr) # remove spaces before comma, period and exclamation etc.
print(mystr)
Output:
Hello . To , the world !
Hello. To, the world!
0
solved String out of index