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

Larry Bates larry.bates at websafe.com`
Mon Jun 16 12:35:52 EDT 2008


Ethan Furman wrote:
> Greetings.
> 
> 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() ;)
> 
> For example:
> .strip --> 'www.example.com'.strip('cmowz.')
> 'example'
> .??? --> --- 'www.example.com'.strip('cmowz.')
> 'exaple'
> -- 
> Ethan
> 
filter()

 >>> removeChars = ';j'
 >>> filter(lambda c: c not in removeChars, x)
'asdfklasdfkl'
 >>>

or

a list comprehension

x="asdfjkl;asdfjkl;"
 >>> ''.join([c for c in x if c not in ';'])
'asdfjklasdfjkl'

-Larry



More information about the Python-list mailing list