Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to fix 0o prefix, especially in date = datetime(2021,07,31) as an input to find day of week in a function in python3?

def day_of_week(date):
    import datetime
    return datetime.datetime.strftime(date, '%A')

date = datetime(2021,07,31)
print(day_of_week(date))

output:
File "", line 5
date = datetime(2021,07,31)
^
SyntaxError: leading zeros in decimal integer literals are not permitted; use an 0o prefix for octal integers

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The error here is that Python 3.0 and later doesn’t like integer literals that start with zero and consist only of digits — specifically, it’s complaining about the 07 integer literal.

The reason this is a problem is that using a 0 prefix on an integer literal used to, especially in the Unix tradition, indicate a number that’s supposed to be interpreted in octal (base-8) instead of decimal (base-10). For example, the Unix command chmod takes an octal value to set the flags for a file (e.g. chmod 0753 foo.txt). In Python you used to be able to do this same thing, but then it’s ambiguous how to interpret an integer literal starting with 0.

So to avoid the ambiguity, if you really want to specify an integer literal using octal notation, starting in Python 3 you must prefix the octal literal with 0o (that’s a zero and a lower-case letter o). Similarly, you can also specify other bases like 0x (hexadecimal, base-16), or 0b (binary, base-2).

>>> 58 == 0x3a == 0o72 == 0b111010
True

More information at https://www.python.org/dev/peps/pep-3127/

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading