[Tutor] Why does the Hex builtin function in Python return a string ?

Py Hex pyhex at yahoo.com
Tue Aug 26 02:45:05 CEST 2008


When I run this:

>>> type(hex(12))
<type 'str'>

I get a string type back, i.e, '0xC' not 0xC

On the other hand, if I use 0x with data, Python understands it is hex data and not a string value.

>>> e = 0xCD
>>> type(e)
<type 'int'>

Why does the Hex builtin function in Python return a string ?  How can I convert this string returned by hex builtin function to data with 0x prefixed ? 

Am I missing anything ? Is there a builtin in Python (I'm using Python 2.5) that does this conversion from int to hex without returning a string value?

It is a bit confusing. 

Thanks 
Ramses


      



More information about the Tutor mailing list