Skip to content Skip to sidebar Skip to footer

Python Replace Space With Special Characters Between Strings

I would like to replace all spaces between strings with '#' except for the space after the end of string. Example: input=' hello world ' output = '#hello##world' I know using

Solution 1:

Use regular expressions.

import re
a = ' hello  world    '
a = re.sub(' +$', '', a)
output = re.sub(' ', '#', a)

but really, this is better:

output = re.sub(' ', '#', a.rstrip())

Post a Comment for "Python Replace Space With Special Characters Between Strings"