Deleting characters from a string

Hrvoje Niksic hniksic at srce.hr
Sun Jun 6 16:09:40 EDT 1999


Skip Montanaro <skip at mojam.com> writes:

> > What I would really like to be able to do is write:
> > 
> >   host = string.translate(host, None, ' "')
> > 
> > Am I missing a more elegant solution?
> 
> Probably less elegant, but you can use string.replace.  You'll have to
> call it twice to get the spaces and the double quotes:
> 
>     host = string.replace(string.replace(host, '"', ""), " ", "")

The problem with this solution is that it requires calling
string.replace() as many times as there are characters to be deleted.
This would not be a problem if not for the fact that string.replace()
creates a new string every time around.  So for many characters
string.translate() should be much more efficient.




More information about the Python-list mailing list