'string'.strip(chars)-like function that removes from the middle?

Raymond Hettinger python at rcn.com
Mon Jun 16 13:25:21 EDT 2008


On Jun 16, 10:09 am, Peter Otten <__pete... at web.de> wrote:
> Ethan Furman wrote:
> > The strip() method of strings works from both ends towards the middle.
> > Is there a simple, built-in way to remove several characters from a
> > string no matter their location? (besides .replace() ;)
> >>> identity = "".join(map(chr, range(256)))
> >>> 'www.example.com'.translate(identity, 'cmowz.')
>
> 'exaple'

And in Py2.6, you'll be able to simplify further:

>>> 'abcde'.translate(None, 'bd')
'ace'


Raymond



More information about the Python-list mailing list