[Tutor] Problem formatting raw_input

Kent Johnson kent37 at tds.net
Fri Oct 31 11:55:07 CET 2008


On Thu, Oct 30, 2008 at 11:03 PM, Peter Anderson <
peter.anderson at internode.on.net> wrote:

> My problem is with the actual entry of the ASCII codes. My solution has a
> "if" test to remove the leading "0" from an input of say "033". With this
> test the output of the decode is correct in printing "!". Without the "if"
> test the output is "\x1b" at best or a run-time error for other numbers.


That is because the leading 0 marks the number as octal when eval()
interprets it as a number.


> My question is this: is there a better way to convert raw input of say
> "033" to "33" than what I have used? Thanks in advance for any help.


Dj's use of int() is the best way to do this. For future reference, the easy
way to strip leading 0's from a string is with lstrip():

In [5]: '033'.lstrip('0')
Out[5]: '33'

Kent
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20081031/0f5c1dc9/attachment.htm>


More information about the Tutor mailing list