[Tutor] Convert string to long

Steven D'Aprano steve at pearwood.info
Thu Feb 24 11:59:30 CET 2011


tee chwee liong wrote:
> hi, 
>  
> is there a way to convert from string to long? 

my_string = "1234"
my_long = long(my_string)

We're happy to help you, but you should make some effort to help 
yourself. Have you worked through the Python tutorial? Don't just *read* 
it, actually follow the instructions and *do* it.

 From Python 2.5 on, the function int() will do the same thing and 
there's never any reason to use long(). From Python 3, long() is removed.

int() (and long) also take an optional second argument, the base to use:

 >>> int("11")  # decimal by default
11
 >>> int("11", 2)  # binary
3


-- 
Steven


More information about the Tutor mailing list