Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why doesn't strip work?
#1
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.
Reply
#2
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.
Reply
#3
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?'
Reply
#4
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?
Reply
#5
(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'
Reply
#6
(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
Recommended Tutorials:
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Why doesn't calling a parent constructor work with arbitrary keyword arguments? PurposefulCoder 4 868 Jun-24-2023, 02:14 PM
Last Post: deanhystad
  Why doesn't this code work? What is wrong with path? Melcu54 7 1,679 Jan-29-2023, 06:24 PM
Last Post: Melcu54
  color code doesn't work harryvl 1 838 Dec-29-2022, 08:59 PM
Last Post: deanhystad
  extract only text strip byte array Pir8Radio 7 2,786 Nov-29-2022, 10:24 PM
Last Post: Pir8Radio
Smile please help me remove error for string.strip() jamie_01 3 1,145 Oct-14-2022, 07:48 AM
Last Post: Pedroski55
  client.get_all_tickers() Doesn't work gerald 2 1,654 Jun-16-2022, 07:59 AM
Last Post: gerald
  pip doesn't work after Python upgrade Pavel_47 10 4,048 May-30-2022, 03:31 PM
Last Post: bowlofred
  Can't strip extra characters from Data Canflyguy 7 1,813 Jan-10-2022, 02:16 PM
Last Post: Canflyguy
  For Loop Works Fine But Append For Pandas Doesn't Work knight2000 2 1,928 Dec-18-2021, 02:38 AM
Last Post: knight2000
  Class Method to Calculate Age Doesn't Work gdbengo 1 1,656 Oct-30-2021, 11:20 PM
Last Post: Yoriz

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020