Urgent: "\" in strings?

Thomas Wouters thomas at xs4all.net
Tue Aug 8 08:44:56 EDT 2000


On Tue, Aug 08, 2000 at 05:32:31PM +0800, Nick Bower wrote:
> I'm messing with PIL, and the only way I can see to get a palette out of
> an image returns a string such as:

> '\000\000\001\002' etc

> which is not that weird until you do a s[0] and it returns:

> '\000' instead of '\

The '\' is an 'escape character', meaning that the character(s) that follow
it have a special meaning. the character '0' means that the next to digits
should be taken as an octal number representing the character. Python
strings display like that when the character they represent isn't normally
'printable'. In other words, the string above consists of two characters of
value 0x0 (not "0", just 0, no bits set), one character of value 0x1 and one
of value 0x2.

> How do I convert this string to numbers?

You want 'ord':

>>> x = "\000\000\001\002"
>>> x
'\000\000\001\002'
>>> x[0]
'\000'
>>> ord(x[0])
0
>>> ord(x[3])
2
>>> 


-- 
Thomas Wouters <thomas at xs4all.net>

Hi! I'm a .signature virus! copy me into your .signature file to help me spread!




More information about the Python-list mailing list