[Tutor] string to integer

bob bgailer at alum.rpi.edu
Thu Jan 5 22:21:55 CET 2006


At 12:40 PM 1/5/2006, Boyan R. wrote:
>I need program to convert my string in integer.
>I remember in BASIC I used  val(string) command
>Is there a Python equivalent ?
>
>Here is how it should work:
>val(7) = 7
>val(bbab7) = 7
>val(aa7aa) = 7
>val(   7) = 7
>
>This last is most important, currently I don't know how to
>convert string "   7" to integer value 7 in my program
>(those empty spaces are nasty)
>btw, I'm working with random numbers, I took 7 just for example :)

int("7") -> 7
int(" 7") -> 7
int("aa7aa") ->ValueError: invalid literal for int(): aa7aa. You'd 
need to remove the non-digits using replace or re.sub.

>what are chr() values for enter and (empty) space ?
>If anybody have a table with chr() values

Are you asking what numeric value passed to chr() gives Enter (etc)? 
If so consult any ASCII Chart. One is at
http://www.lookuptables.com/. Of course Enter in ASCII is CR. 



More information about the Tutor mailing list