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 rstrip()
I can neglect the space at the end of string . I just want to try without using rstrip()
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())
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.