0

I am trying to retrieve Images in a folder based on indices retrieved from a list. The images format is frame%04d whereas list consists of positive integers. when I passed the following statement 41 == 0041, the result is false. What is the correct procedure?

DeepSpace
  • 78,697
  • 11
  • 109
  • 154
  • 3
    You might want to have a look at [What do numbers starting with 0 mean in Python](https://stackoverflow.com/questions/11620151/what-do-numbers-starting-with-0-mean-in-python). Literal numbers starting with 0 tell Python that your radix is 8 (octal). So 0041 is really 33 in decimal, and obviously 41 and 33 are not equal. – lurker Mar 14 '19 at 12:18
  • That looks like false to me – Ingus Mar 14 '19 at 12:20
  • In addition to @lurker's comment, `frame41` is a string, as is `frame0041`. The two are not equal, so `false` is the correct result in any language even if there wasn't a special meaning to numbers starting with `0`. – Ken White Mar 14 '19 at 12:23
  • @lurker Thanks for the clarification issue is resolved – Annamaneni Sriharsha Mar 14 '19 at 12:28
  • @lurjer BTW In Python 3, octal literals must begin with `0o`. `041` is an invalid syntax. This question was tagged with Python 3.x, I changed it to Python 2.x – DeepSpace Mar 14 '19 at 12:29
  • @DeepSpace ah good catch. Thanks. – lurker Mar 14 '19 at 12:35

1 Answers1

0

In Python3, it is not even possible to have integer with leading zeros:

>>> foo = 0041
SyntaxError: invalid token

In Python2, as DeepSpace pointed out, having leading zeros refers to octal literals:

>>> 0041
33
>>> 0081
SyntaxError: invalid token

Therefore, if you are comparing strings, you can easily do that with

>>> "41" == "0041".lstrip("0")
True

which results to True