Last updated: Apr 8, 2024
Reading timeΒ·3 min
The Python "SyntaxError: leading zeros in decimal integer literals are not permitted" occurs when we declare an integer with leading zeros.
To solve the error, remove any leading zeros from the integer or wrap the value in a string.
Here is an example of how the error occurs.
# βοΈ SyntaxError: leading zeros in decimal integer literals are not permitted; use an 0o prefix for octal integers my_num = 08
It's not allowed to have leading zeros in an integer in Python.
One way to solve the error is to remove all leading zeros.
my_num = 8 print(my_num) # ποΈ 8
Removing the leading zero from the integer resolved the issue.
# βοΈ Causes error my_num = 007 # -------------------------------- # β Works as intended my_num = 7
Numbers cannot begin with leading zeros and converting a string that contains leading zeros to a number automatically drops the leading zeros.
my_str = '0007' my_int = int(my_str) print(my_int) # ποΈ 7
The only integer that can start with 0
in Python is 0
itself.
my_int = 0 print(my_int) # ποΈ 0
Alternatively, you can wrap the value in a string.
my_num = '08' print(my_num) # ποΈ '08'
When the value is wrapped in quotes, it is a string and can start with leading zeros.
You can use a formatted string literal if you need to pad the value to a certain length with leading zeros.
my_int = 7 result = f'{my_int:03}' print(result) # ποΈ 007 result = f'{my_int:04}' print(result) # ποΈ 0007 result = f'{my_int:05}' print(result) # ποΈ 00007
If you need to format a list of integers to N leading zeros, use a list comprehension.
a_list = [3, 5, 7, 9] new_list = [f'{item:03}' for item in a_list] print(new_list) # ποΈ ['003', '005', '007', '009']
List comprehensions are used to perform some operation for every element or select a subset of elements that meet a condition.
Formatted string literals (f-strings) let us include expressions inside of a
string by prefixing the string with f
.
var1 = 'bobby' var2 = 'hadz' result = f'{var1}{var2}' print(result) # ποΈ bobbyhadz
Make sure to wrap expressions in curly braces - {expression}
.
Formatted string literals also enable us to use the format specification mini-language in expression blocks.
The 0
after the colon is the pad character.
my_int = 7 result = f'{my_int:03}' print(result) # ποΈ 007
The examples pad the string to a length of 3, 4 or 5 with leading zeros if necessary.
If the string is already of the specified length, no leading zeros are added.
The same applies to dictionary keys and list items.
# βοΈ SyntaxError: leading zeros in decimal integer literals are not permitted; use an 0o prefix for octal integers my_dict = {04: 'hi'} # β οΈSyntaxError: leading zeros in decimal integer literals are not permitted; use an 0o prefix for octal integers my_list = [01, 02]
You can wrap the dictionary key in a string or drop the leading zero.
my_dict = {'04': 'hi'} print(my_dict['04']) # ποΈ 'hi'
Make sure to wrap all list elements in quotes if they start with leading zeros.
my_list = ['01', '02'] print(my_list[0]) # ποΈ '01' print(my_list[1]) # ποΈ '02'
If you meant to specify an octal literal, prefix it with 0o
.
example_1 = 0o72 print(example_1) # ποΈ 58 example_2 = 0o73 print(example_2) # ποΈ 59
The octal notation has 8
rather than 10
as a base.
You can learn more about the related topics by checking out the following tutorials: