1

It seems like a trivial task however, I can't find a solution for doing this using python.

Given the following string:

"Lorem/ipsum/dolor/sit         amet consetetur"

I would like to output

"Lorem/ipsum/dolor/sit         ametconsetetur"

Hence, removing the single whitespace between amet and consetetur.

Using .replace(" ","") replaces all whitespaces, giving me:

"Lorem/ipsum/dolor/sitametconsetetur"

which is not what I want. How can I solve this?

AaronDT
  • 3,940
  • 8
  • 31
  • 71

3 Answers3

3

use regex and word boundary:

>>> s="Lorem/ipsum/dolor/sit         amet consetetur"
>>> import re
>>> re.sub(r"\b \b","",s)
'Lorem/ipsum/dolor/sit         ametconsetetur'
>>>

This technique also handles the more general case:

>>> s="Lorem/ipsum/dolor/sit         amet consetetur      adipisci velit"
>>> re.sub(r"\b \b","",s)
'Lorem/ipsum/dolor/sit         ametconsetetur      adipiscivelit'

for start & end spaces, you'll have to work slightly harder, but it's still doable:

>>> s=" Lorem/ipsum/dolor/sit         amet consetetur      adipisci velit "
>>> re.sub(r"(^|\b) (\b|$)","",s)
'Lorem/ipsum/dolor/sit         ametconsetetur      adipiscivelit'

Just for fun, a last variant: use re.split with a multiple space separation, preserve the split char using a group, then join the strings again, removing the spaces only if the string has some non-space in it:

"".join([x if x.isspace() else x.replace(" ","") for x in re.split("( {2,})",s)])

(I suppose that this is slower because of list creation & join though)

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
  • You can also use: [re.sub()](https://docs.python.org/3/library/re.html#re.sub) `text = "Lorem/ipsum/dolor/sit amet consetetur"` `re.sub(" ", "", text, 1)` – maguri Oct 26 '18 at 19:17
  • err, no that doesn't work. Have you tried it? it'll replace one of the many spaces that OP doesn't want to replace, leaving the space between amet and consetetur intact... – Jean-François Fabre Oct 26 '18 at 19:19
1
s[::-1].replace(' ', '', 1)[::-1]
  • Reverse the string
  • Delete the first space
  • Reverse the string back
Owen
  • 919
  • 5
  • 11
  • 1
    This might work in this very specific case, but is in no way a general solution. – Eli Korvigo Oct 26 '18 at 19:14
  • 1
    Yes, of course, I'm not trying to solve a general problem. The OP didn't provide sufficient information to determine what the 'general problem' is, in this case. If you read the question, you'll see they're asking for replacing a single whitespace, which this does. – Owen Oct 26 '18 at 19:17
  • 1
    your solution replaces the _last_ whitespace. It would replace the last whitespace in multiple-space string if it was in the end too. OP example isn't well chosen, agreed but question is clear "replace a single whitespace without replacing multiple whitespaces" – Jean-François Fabre Oct 26 '18 at 19:21
0
import re
a="Lorem/ipsum/dolor/sit         amet consetetur"
print(re.sub('(\w)\s{1}(\w)',r'\1\2',a))
mad_
  • 8,121
  • 2
  • 25
  • 40
  • Thank you for the code snippet, which might provide some limited, immediate help. A proper explanation would greatly improve its [long-term value](https://meta.stackexchange.com/q/114762/206345) by describing why this is a good solution to the problem, and would make it more useful to future readers with other similar questions. Please edit your answer to add some explanation, including the assumptions you've made. – sepehr Oct 26 '18 at 21:51