Python 3.6+ Lets You Use Underscores to Make Large Numbers Readable
Python has supported underscore separators in numeric literals since version 3.6, allowing numbers like 1234567890 to be written as 1_234_567_890 for easier reading. The feature works for integers and floats, though underscores cannot appear at the start or end of a number, adjacent to a decimal point, or placed consecutively. Using commas instead — a common mistake — causes Python to interpret the value as a tuple, leading to subtle bugs. To display an existing number with underscore formatting, developers can use Python's f-string syntax with the underscore format specifier. Since code is read far more often than it is written, adopting this convention can reduce errors and improve clarity for both developers and their teammates.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.
Discussion (0)
Log in to join the discussion and vote.
Log in