HOWTO · Python
How to Fix the SyntaxError: Invalid Token in Python
Understand why Python 3 rejects leading zeroes in decimal integer literals and choose strings, integers, or dates for date-like values.
On this page
In Python 3, 0 is a valid decimal integer literal, but nonzero decimal integer literals cannot have leading zeroes: 04 and 08 cause a SyntaxError. Use 4 and 8 when you need numbers, quoted strings such as "04" and "08" when the zeroes are part of the display, or a date type for calendar operations.
Why Python reports an invalid token
Python reads source code as a sequence of tokens, such as names, operators, punctuation, and literals. A literal is a value written directly in the source. The characters 04 may look like the number four with a display prefix, but they are not a valid Python 3 decimal integer literal. As a result, a tuple such as (04, 08, 2022) cannot be parsed, so its print call never runs.
The phrase SyntaxError: invalid token appears in older Python diagnostics and in some environments. Current Python 3 versions may report a different message, such as leading zeros in decimal integer literals are not permitted; wording varies by interpreter version. In either case, the issue is the spelling of the integer literal, not whitespace between tokens or the print function. Python’s integer literal rules describe the accepted forms.
Fix leading-zero integer literals in Python 3
For decimal numbers, remove the leading zero: use 4 and 8. Python treats these as ordinary integers. If you meant an octal value, Python 3 uses the 0o prefix, as in 0o10; 08 is not a valid octal spelling. Hexadecimal notation starts with 0x, but changing a date component to hexadecimal does not preserve its intended display or make it a date.
When the leading zero is part of how a value should be displayed, store the component as text. This example keeps the day and month formatting in a tuple:
date_parts = ("04", "08", "2022")
print(date_parts)
The tuple contains three strings, so the leading zeroes remain part of the values. It is suitable for simple display or input handling, but the components are not numeric and do not provide calendar validation. Converting a string such as "04" to an integer gives 4; the original formatting is then gone unless you format it again.
Choose strings, integers, or dates based on what the values mean
Choose the representation according to the task. Use integers when arithmetic or numeric comparison matters: the day and month components can be 4 and 8. Use strings such as "04" and "08" when fixed-width display matters, for example in a form, file name, or manually assembled label. A dictionary can make named text fields clearer, using keys such as "Day", "Month", and "Year"; it still stores strings and does not check whether the combination is a real date.
For calendar operations, use a date type instead of treating a date as an arbitrary tuple. Python’s standard datetime.date represents a year, month, and day and validates the calendar date when constructed. For example, a value conceptually equivalent to date(2022, 8, 4) stores numeric components for calendar work; formatting it for display can add leading zeroes without changing those components. Keep in mind that date formatting and integer-literal syntax solve different problems: formatting controls presentation after values exist, while valid literal spelling is required for Python to parse the source at all.