Faqts : Business : Programming : Shopping For You : Python : Common Problems : Strings

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

8 of 13 people (62%) answered Yes
Recently 6 of 10 people (60%) answered Yes

Entry

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

Dec 18th, 2009 15:23
new acct, unknown unknown, Ken Seehof


>>> "s\240p\240a\240m".replace("\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 = s.replace("\240", "") 
>>> print s 
spam