1

I want to write code that will remove extraneous spaces in a string. Any more than 1 space in between words would be an extraneous space. I want to remove those spaces but keep 1 space in between words

I've written code that will remove spaces at the beginning and the end but I'm not sure for to make it remove the middle spaces but keep 1 there.

#Space Cull
def space_cull(str):
  result = str 
  result = result.strip()
  return result

So this is what my code does right now

space_cull('    Cats   go   meow   ')
#It would return
'Cats   go   meow'

What I want it to do is this:

space_cull('    Cats   go    meow')
#It would return
'Cats go meow'

How should I do this?

KittyWhale
  • 89
  • 7
  • 4
    `' '.join(' Cats go meow '.split())` – Klaus D. Oct 31 '19 at 14:57
  • Check this answer @KittyWhale: https://stackoverflow.com/questions/8270092/remove-all-whitespace-in-a-string-in-python – razimbres Oct 31 '19 at 14:59
  • 4
    Why is someone downvoting all answers?? – Nils Oct 31 '19 at 15:00
  • Thank you so much! – KittyWhale Oct 31 '19 at 15:00
  • Also idk why everyone is down voting all the answers they're very helpful. – KittyWhale Oct 31 '19 at 15:01
  • 1
    @Nils, It's always a touchy subject on Stack to answer trivial questions: https://meta.stackoverflow.com/questions/255459/is-it-okay-to-downvote-answers-to-bad-questions. IMO, They shouldn't be downvoted but I think it's best to do what Klaus did and just post the 1-liner trivial answer as a comment. People are free to answer whatever they want but they should know some people frown upon it. – Brian Oct 31 '19 at 15:02
  • Possible duplicate of [Remove all whitespace in a string in Python](https://stackoverflow.com/questions/8270092/remove-all-whitespace-in-a-string-in-python) – Peter Wood Oct 31 '19 at 15:04
  • My guess is that some people think this question should be closed instead of being answered and are downvoting all answers to discourage people from answering questions like this or out of pettiness. – JBGreen Oct 31 '19 at 15:04
  • 2
    Well, thanks to that pettiness I got me a bronze badge. Thanks guys! – Mad Physicist Oct 31 '19 at 15:08
  • @Peter. That's not a duplicate. Retaining specific spaces is a simple, but absolutely not trivial modification at the beginner level – Mad Physicist Oct 31 '19 at 15:10
  • @MadPhysicist maybe the question isn't exactly the same, but the [accepted answer](https://stackoverflow.com/a/8270146/1084416) is comprehensive. – Peter Wood Oct 31 '19 at 15:12
  • but this is different from that other question that some are recommending, that question is asking how to removed ALL spaces, I want to keep 1 space between the words instead of removing all space. Thank you all again though! – KittyWhale Oct 31 '19 at 15:15

4 Answers4

4

It works like this:

sentence = '    Cats   go   meow   '
" ".join(sentence.split())
Nils
  • 910
  • 8
  • 30
  • 2
    I don't know why this was downvoted. You need a strip before or after, but seems like a reasonable approach – Mad Physicist Oct 31 '19 at 15:00
  • @MadPhysicist when executing my code I get the desired result. So there is no need to use an additional strip, or am I mistaken somehow? – Nils Oct 31 '19 at 15:03
  • Sure :D even though I want to note that I feel bad doing it. – Nils Oct 31 '19 at 15:07
  • 1
    I just had to add sentence = to the beginning so that It stored that data and then could return it, but it works great! (and i actually understand how it works lol) – KittyWhale Oct 31 '19 at 15:12
  • If you work with strings regularly you should start looking into regex though. It is a library made for working with strings and is a lot more powerful than the built-in methods. – Nils Oct 31 '19 at 15:14
2

You can use re.sub to replace any number of spaces with a single space:

>>> import re
>>> re.sub(r"\s+", " ", "foo    bar")
"foo bar"
JBGreen
  • 534
  • 2
  • 6
0

you can do :

txt = '    Cats   go   meow   '

def space_cull(string):

    word = string.split(" ")
    result = ""
    for elem in word:
        if not elem == '':
            result += str(elem) + ' '
    return result.strip()

print(space_cull(txt))

output:

Cats go meow
Alexall
  • 423
  • 2
  • 12
-1

You can use built-in string methods:

x = "  cats    go    meow     "
print(*x.strip().split())

Output will be:

cats go meow
Seysen
  • 134
  • 11
  • Why do that when you can just do `print(*x.split())` for the same print statement? And in any case, that just prints the value as output, when the value under the hood is still a list. If OP wants to return a string without extra spaces (not a list), then your solution would need a ' '.join(...) to be complete. – Brian Oct 31 '19 at 15:12