faqts : Computers : Programming : Languages : Python : Common Problems : Strings

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

7 of 12 people (58%) answered Yes
Recently 5 of 10 people (50%) answered Yes

Entry

How can I strip unwanted characters out of my string? Octal 240 for instance.

Jun 5th, 2000 19:50
unknown unknown, Ken Seehof


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



But 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