[Tutor] Remove specific chars from a string

Ricardo Aráoz ricaraoz at gmail.com
Tue Apr 15 16:42:23 CEST 2008


> Malcolm Greene wrote:
>> What is the Pythonic way to remove specific chars from a string? The
>> .translate( table[, deletechars]) method seems the most 'politically
>> correct' and also the most complicated.

Why complicated?

<code>
import string
myStr = 'some text from which you want to delete some chars'
resultStr = s.translate(string.maketrans('', ''), 'nem')
print resultStr
</code>
Output : so txt fro which you wat to dlt so chars

You just have to use maketrans with empty strings. If string is 
deprecated then I guess some other way of creating translation tables 
will be provided.

HTH



More information about the Tutor mailing list