How to replace a string with a backspace?

str.replace() allows to replace a substring in a string with something else.

I have the problem of repeated whitespaces after the replacement:

>>> 'word word to be removed word'.replace('to be removed', '')
'word word  word'

Note the two whitespaces after the second word.

Is there a way to replace a substring with a backspace that would remove one space? (I know that the string will be made of words separated by spaces)



Read more here: https://stackoverflow.com/questions/66338571/how-to-replace-a-string-with-a-backspace

Content Attribution

This content was originally published by WoJ at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: