Mar-22-2023, 10:15 PM
Mar-22-2023, 10:27 PM
I'm sure the syntax error clearly stated the problem.
x = 01
Error: File ..., line 1
x = 01
^
SyntaxError: leading zeros in decimal integer literals are not permitted; use an 0o prefix for octal integers
Mar-22-2023, 10:36 PM
That is correct! That is the error.
I'm confused why it is erroring out.
1 and 01 are the same numbers. I'm trying to match some lines from different files, one file has single digits and a second file has zero leading digits. I can stip zeros but I thought I could directly compare those...
I guess I cannot.
Thank you!
I'm confused why it is erroring out.
1 and 01 are the same numbers. I'm trying to match some lines from different files, one file has single digits and a second file has zero leading digits. I can stip zeros but I thought I could directly compare those...
I guess I cannot.
Thank you!
Mar-22-2023, 10:53 PM
Your are not allowed to put leading zeros in front of a number in Python, This is clearly stated by the error message.
Python does not care much about what you or I think should be allowed. You have to follow the Python rules if you want to play.
You do not need to "strip" the zeros. Python only complains about leading zeros in "integer literals". Integer literals are number strings that are part of code. Python has no problem with leading zeros in strings:
test.py
Quote:leading zeros in decimal integer literals are not permitted
Python does not care much about what you or I think should be allowed. You have to follow the Python rules if you want to play.
You do not need to "strip" the zeros. Python only complains about leading zeros in "integer literals". Integer literals are number strings that are part of code. Python has no problem with leading zeros in strings:
print(int("01") == 1)
Output:True
Or in user input.test.py
print(int(input("Enter 01: ")) == 1)
Output:(venv) >python test.py
Enter 01: 01
True
Mar-22-2023, 11:49 PM
Got it!
Thank you!
Thank you!
Mar-23-2023, 12:05 AM
(Mar-22-2023, 10:36 PM)tester_V Wrote: [ -> ]1 and 01 are the same numbers.You might hope that (and generally for 01, you're correct). But in some contexts a leading zero is significant and changes the meaning.
In python2, a leading zero was an indication for an octal literal.
Python 2.7.15 (default, Jan 24 2020, 13:07:12) [GCC 4.2.1 Compatible Apple LLVM 10.0.1 (clang-1001.0.46.4)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> 21 21 >>> 021 17This can be unexpected. So this format is not used in python3. Now an octal literal requires the explicit
0o
prefix. Meanwhile a plain leading zero was made illegal so there's no chance old code that expected it to be an octal will run incorrectly.Mar-23-2023, 10:47 AM
XY-Problem?
Do you want to compare serial numbers or postal codes?
e.G. in Germany, we have postal-codes with 5 digits and some of them with a leading zero.
Is equal:
The opposite is, if you have converted e.G. a postal-code to int and you print it, you get a different postal-code:
Do you want to compare serial numbers or postal codes?
e.G. in Germany, we have postal-codes with 5 digits and some of them with a leading zero.
Is equal:
int("01234") == int("1234")Is not equal:
"01234" == "1234"It's better not to convert a
str
to int
, if the leading zero has a meaning.The opposite is, if you have converted e.G. a postal-code to int and you print it, you get a different postal-code:
print(int("042"))