>> str(055) '45' ...">

Python 2.7 str (055) returns "45" instead of 055

Why am I getting the following result in python 2.7 instead of '055'?

>>> str(055)
'45'
+4
source share
1 answer

055- an octal number whose decimal equivalent 45, use octto get the correct output.

>>> oct(055)
'055'

The syntax for octal numbers in Python 2.X is:

octinteger     ::=  "0" ("o" | "O") octdigit+ | "0" octdigit+

But this is only for presentation purposes, ultimately they are always converted to integers for storage or calculation:

>>> x = 055
>>> x
45
>>> x = 0xff   # HexaDecimal
>>> x
255
>>> x = 0b111  # Binary
>>> x
7
>>> 0xff * 055
11475

Note that in octal numbers, Python 3.x is now displayed 0o. Thus, using 055, will increase SyntaxError.

+11

Source: https://habr.com/ru/post/1524189/


All Articles