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?

9
  • 2
    Most of us would use a regular expression (regex) to find a single space surrounded by non-spaces. Feed "Python regex" to your search engine and pick a handy tutorial. Commented Oct 26, 2018 at 19:11
  • 1
    .. or wait about 15 seconds ... Commented Oct 26, 2018 at 19:13
  • 1
    @timgeb that doesn't mean that this isn't a duplicate. Just, not of that one. Commented Oct 26, 2018 at 19:16
  • 1
    Well, then it's reopened until someone finds a better target. :) Commented Oct 26, 2018 at 19:17
  • 1
    @timgeb wise decision. I tried to look for a better target but google wasn't my friend. There may be one, but well hidden. If someone finds a good dupe, just ping me I'll close Commented Oct 26, 2018 at 19:20

3 Answers 3

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)

Sign up to request clarification or add additional context in comments.

2 Comments

You can also use: re.sub() text = "Lorem/ipsum/dolor/sit amet consetetur" re.sub(" ", "", text, 1)
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...
1
s[::-1].replace(' ', '', 1)[::-1]
  • Reverse the string
  • Delete the first space
  • Reverse the string back

3 Comments

This might work in this very specific case, but is in no way a general solution.
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.
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"
0
import re
a="Lorem/ipsum/dolor/sit         amet consetetur"
print(re.sub('(\w)\s{1}(\w)',r'\1\2',a))

1 Comment

Thank you for the code snippet, which might provide some limited, immediate help. A proper explanation would greatly improve its long-term value 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.