0

So when I do something like this :

    x=str(19072000)
    day=x[:2]
    month=x[2:4]
    year=x[4:]
    final=day+"/"+month+"/"+year
    print(final)
I get:19/07/2000

>>>x=str(01011930)
File"(Stdin)",line 1
x=str(01011930)
                       ^
SyntaxError:invalid token

But when i try to do the same thing but with 01011930 i get SyntaxError:invalid token,any ideas why?

0

1 Answer 1

1

The problem is the leading 0 in your integer constant.

In Python 2, an integer that begins with a 0 is taken to be an octal constant, which doesn't allow the digits 8 or 9 (and even without them, the resulting number will be quite different from what you intend).

In Python 3, it isn't allowed at all, except for 0 itself. (Octal constants begin with 0o in Python 3, similar to how hex constants begin with 0x.)

To do what you want, just remove the leading zero. Then you can zero-pad using:

x = "%08d" % 1011930

This will set x to the string '01011930'. Then your indexing will work.

Equivalently, you can use:

x = "{:08d}".format(1011930)
Sign up to request clarification or add additional context in comments.

2 Comments

When i try to run the code with your solution in command line it works,however when i try to do it in the eclipse it still gives me: r1=Radnik("Petar","Petrovic",1907000820015,01011930 ,"Senta","Srbija") ^ SyntaxError: invalid token I have a class written for this,it works with any other date/numbers but with anything that starts with 0 it still won't work.
You didn't remove the leading zero, it's still there: 01011930. You need to remove the leading zeros from all of your integer constants. Change it to 1011930. The leading zeros are causing your syntax errors.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.