stripping out unwanted characters in string

Ken Seehof 12klat at sightreader.com
Mon Jun 5 09:22:55 EDT 2000


"Jerry F. Davis" wrote:

> Howdy:
>
> I have a string that has some unwanted characters in it.
> Octal 240 for instance.

>>> import string
>>> string.replace("s\240p\240a\240m","\240","")
'spam'

> I want to actually strip them out of my string.

Can't exactly do that.  Strings are immutable.  If you don't know what
I mean by immutable, find out (mutability is a key concept of python).

(Hint: if strings were not immutable, they could not be safely used as
keys in dictionaries.)

So you'll have to settle for:

>>> s = "s\240p\240a\240m"
>>> s = string.replace(s, "\240", "")
>>> print s
spam

> I have looked at string.replace, but can't seem to figure out how to do
> this.
>
> Thanks in advance.
>
> Jerry
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20000605/10d16de1/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 12klat.vcf
Type: text/x-vcard
Size: 343 bytes
Desc: Card for Ken Seehof
URL: <http://mail.python.org/pipermail/python-list/attachments/20000605/10d16de1/attachment.vcf>


More information about the Python-list mailing list