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

Peter Otten __peter__ at web.de
Tue Jun 17 03:42:59 EDT 2008


Terry Reedy wrote:

> Cédric Lucantis wrote:
> 
>> I don't see any string method to do that
> 
>  >>> 'abcde'.translate(str.maketrans('','','bcd'))
> 'ae'
> 
> I do not claim this to be better than all the other methods,
> but this pair can also translate while deleting, which others cannot.

You should mention that you are using Python 3.0 ;) 
The 2.5 equivalent would be

>>> u"abcde".translate(dict.fromkeys(map(ord, u"bcd")))
u'ae'

Peter



More information about the Python-list mailing list