Laurenz
Laurenz

Reputation: 1960

Where does it say that 0x prefix makes literals hex base?

I know that I can write 0x1230FF in Python and it will be a literal int with the value 1192191. But I can't find anything in the official docs or the PEPs that specifies that this is allowed and that 0x indeed denotes a hex radix.

The closest I could find was PEP-3127 Integer Literal Support and Syntax, which mostly talks about octal and binary. It makes some interesting suggestions like

Suggestions were made to use a "c" (the second letter of "oCtal"), or even to use a "t" for "ocTal" and an "n" for "biNary" to go along with the "x" for "heXadecimal".

Can somebody provide a link to an official specification of this?

Note that I am not talking about string formatting, but syntax for Number literals.

Upvotes: 0

Views: 2485

Answers (1)

khelwood
khelwood

Reputation: 59166

The docs here: https://docs.python.org/3/reference/lexical_analysis.html#integer-literals

specify that a hexadecimal integer literal is made up of an 0 followed by an x or X, followed by one or more hexadecimal digits and underscores.

hexinteger   ::=  "0" ("x" | "X") (["_"] hexdigit)+

Upvotes: 2

Related Questions