Python Forum
Why doesn't strip work? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Why doesn't strip work? (/thread-14956.html)



Why doesn't strip work? - cybervigilante - Dec-26-2018

import string
dogs = "All.Good,Dogs.. Eat.! Shoes?"
dogs = dogs.strip(string.punctuation)
print(dogs)
My result is All.Good,Dogs.. Eat.! Shoes
with no punctuation removed. Correction: The ? disappeared but I thought all punctuation would go.


RE: Why doesn't strip work? - stullis - Dec-26-2018

str.strip() only removes characters or space from the ends of the string. Notice that the question mark is removed from your string.

You want to use str.replace() to remove interior characters.


RE: Why doesn't strip work? - snippsat - Dec-26-2018

Can do it like this.
>>> from string import punctuation
>>> 
>>> s = "All.Good,Dogs.. Eat.! Shoes?"
>>> ''.join(c for c in s if c not in punctuation)
'AllGoodDogs Eat Shoes'
Or specify yourself what to remove.
>>> s = "All.Good,Dogs.. Eat.! Shoes?"
>>> "".join(c for c in s if c not in list(',.!'))
'AllGoodDogs Eat Shoes?'



RE: Why doesn't strip work? - cybervigilante - Dec-27-2018

Thanks all. Pydoc is Really unclear on that. It makes it look like it replaces all characters.
strip(...)
S.strip([chars]) -> str

Return a copy of the string S with leading and trailing
whitespace removed.
If chars is given and not None, remove characters in chars instead.

(Dec-26-2018, 10:28 PM)snippsat Wrote: Can do it like this.
>>> from string import punctuation
>>> 
>>> s = "All.Good,Dogs.. Eat.! Shoes?"
>>> ''.join(c for c in s if c not in punctuation)
'AllGoodDogs Eat Shoes'
Or specify yourself what to remove.
>>> s = "All.Good,Dogs.. Eat.! Shoes?"
>>> "".join(c for c in s if c not in list(',.!'))
'AllGoodDogs Eat Shoes?'

That looks like a list comprehension. I thought you had to use square brackets for that. ie:
import string
dogs = 'My., bad? dog - eats,,.. shoes*'
''.join([dog for dog in dogs if dog not in string.punctuation])

By the way, where two words were joined with punctuation and no space they were mashed together, but if I join with spaces I get too many. What's the best way to turn multiple spaces into one?


RE: Why doesn't strip work? - snippsat - Dec-27-2018

(Dec-27-2018, 12:51 AM)cybervigilante Wrote: That looks like a list comprehension. I thought you had to use square brackets for that. ie:
It will be a generator expression.
>>> import string
>>> 
>>> dogs = 'My., bad? dog - eats,,.. shoes*'
>>> (dog for dog in dogs if dog not in string.punctuation)
<generator object <genexpr> at 0x03EEB830>

So eg calling join(), list() on will give result.
list comprehension will work,but needed in this case.
Quote:By the way, where two words were joined with punctuation and no space they were mashed together, but if I join with spaces I get too many. What's the best way to turn multiple spaces into one?
If it always same amount of spaces can use replace().
>>> 'My bad dog  eats shoes'.replace('  ', '')
'My bad dogeats shoes'
With different amount spaces i would use regex.
>>> import re
>>> 
>>> dogs = 'My   bad dog  eats      shoes'
>>> re.sub(' +',' ', dogs)
'My bad dog eats shoes'



RE: Why doesn't strip work? - metulburr - Dec-27-2018

(Dec-27-2018, 12:51 AM)cybervigilante Wrote: By the way, where two words were joined with punctuation and no space they were mashed together, but if I join with spaces I get too many. What's the best way to turn multiple spaces into one?
This method would translate the target punctuation to spaces, then split the string by spaces and rejoin them to make one space between each word.
def remove_punc(s):
    from_ = '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'
    to = '                                '
    translation = str.maketrans(from_,to)
    new = s.translate(translation)
    return ' '.join(new.split())
    
print(remove_punc("All.Good,Dogs.. Eat.! Shoes?"))
print(remove_punc('My., bad? dog - eats,,.. shoes*'))
Output:
All Good Dogs Eat Shoes My bad dog eats shoes