Python Forum
comparing zero leading number with a number
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
comparing zero leading number with a number
#1
Greetings!
I'm comparing 1 with 01 or 2 with 02 and the snippet errors out.
I'm puzzled why.
d = 1
dd = 01
print(d==dd)
Thank you!
Reply
#2
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
Reply
#3
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!
Reply
#4
Your are not allowed to put leading zeros in front of a number in Python, This is clearly stated by the error message.

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
tester_V likes this post
Reply
#5
Got it!
Thank you!
Reply
#6
(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
17
This 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.
tester_V likes this post
Reply
#7
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:
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"))
tester_V likes this post
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Brick Number stored as text with openpyxl CAD79 2 442 Apr-17-2024, 10:17 AM
Last Post: CAD79
  [SOLVED] Pad strings to always get three-digit number? Winfried 2 350 Jan-27-2024, 05:23 PM
Last Post: Winfried
  Prime number detector Mark17 5 811 Nov-27-2023, 12:53 PM
Last Post: deanhystad
  Create X Number of Variables and Assign Data RockBlok 8 957 Nov-14-2023, 08:46 AM
Last Post: perfringo
  find the sum of a series of values that equal a number ancorte 1 502 Oct-30-2023, 05:41 AM
Last Post: Gribouillis
  capturing multiline output for number of parameters jss 3 824 Sep-01-2023, 05:42 PM
Last Post: jss
  Sequential number for rows retrieved and storing the Primary UKey to the line number GYKR 2 582 Aug-22-2023, 10:14 AM
Last Post: GYKR
  doubling a number Skaperen 8 1,247 Jul-25-2023, 10:20 PM
Last Post: Skaperen
Question Extracting Version Number from a String britesc 2 1,097 May-31-2023, 10:20 AM
Last Post: britesc
  Formatting float number output barryjo 2 922 May-04-2023, 02:04 PM
Last Post: barryjo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020