tags:

views:

90

answers:

4

I want to remove any brackets from a string. Why doesn't this work properly?

>>> name = "Barack (of Washington)"
>>> name = name.strip("(){}<>")
>>> print name
Barck (of Washington
+2  A: 

strip only strips characters from the very front and back of the string.

To delete a list of characters, you could use the string's translate method:

import string
name = "Barack (of Washington)"
table = string.maketrans( '', '', )
print name.translate(table,"(){}<>")
# Barack of Washington
unutbu
+1  A: 

I did a time test here, using each method 100000 times in a loop. The results surprised me. (The results still surprise me after editing them in response to valid criticism in the comments.)

Here's the script:

import timeit

bad_chars = '(){}<>'

setup = """import re
import string
s = 'Barack (of Washington)'
bad_chars = '(){}<>'
rgx = re.compile('[%s]' % bad_chars)"""

timer = timeit.Timer('o = "".join(c for c in s if c not in bad_chars)', setup=setup)
print "List comprehension: ",  timer.timeit(100000)


timer = timeit.Timer("o= rgx.sub('', s)", setup=setup)
print "Regular expression: ", timer.timeit(100000)

timer = timeit.Timer('for c in bad_chars: s = s.replace(c, "")', setup=setup)
print "Replace in loop: ", timer.timeit(100000)

timer = timeit.Timer('s.translate(string.maketrans("", "", ), bad_chars)', setup=setup)
print "string.translate: ", timer.timeit(100000)

Here are the results:

List comprehension:  0.631745100021
Regular expression:  0.155561923981
Replace in loop:  0.235936164856
string.translate:  0.0965719223022

Results on other runs follow a similar pattern. If speed is not the primary concern, however, I still think string.translate is not the most readable; the other three are more obvious, though slower to varying degrees.

JasonFruit
Why don't u go for timeit !!!
Tumbleweed
Should have. Didn't.
JasonFruit
Two things: (1) Your replace in loop solution does not actually do anything (you have to write o = o.replace(c, ""). (2) To test regex you should cache the regex pattern: char_re = re.compile('[%s]' % bad_chars) ...
Mike Axiak
Also, I suspect the translate will be by far the fastest (see unutbu's answer)
Mike Axiak
My mistake, Mike. That was pretty weak, but it was within 20 minutes of waking. I'll edit.
JasonFruit
There, @Mike Axiak; parental duties prevented me from finishing my edit until now.
JasonFruit
thanks for this - educative question, not only did I learn that strip() doesn't do what I thought, I also learned three other ways to achieve what I wanted, and which was the fastest!
AP257
+6  A: 

Because that's not what strip() does. It removes leading and trailing characters that are present in the argument, but not those characters in the middle of the string.

You could do:

name= name.replace('(', '').replace(')', '').replace ...

or:

name= ''.join(c for c in name if c not in '(){}<>')

or maybe use a regex:

import re
name= re.sub('[(){}<>]', '', name)
bobince
A: 

Because strip() only strips trailing and leading characters, based on what you provided. I suggest:

>>> import re
>>> name = "Barack (of Washington)"
>>> name = re.sub('[\(\)\{\}<>]', '', name)
>>> print(name)
Barack of Washington
Ruel
In a regex character class you don't need to escape anything, so '[(){}<>]' is fine
Mike Axiak