5
from datetime import datetime current_time = datetime.now() print "%s-%s-%s" % (current_time.month, current_time.day current_time.year,) #Will print the current date in a mm/dd/yyyy format. input() 

The code above is meant to print out the current date in a mm/dd/yyyy format in a command prompt. So for example if this actually worked it would open up a command prompt window that printed out; for example the current date as im writing this like this: 8-5-2017

I keep getting this error when trying to run the module that the closing " in "%s-%s-%s" that it's invalid syntax. Is python 3 using something different from this or did I make a mistake?

0

1 Answer 1

17

In python3, print statements require braces.

Furthermore, if you're looking to print the data, just use datetime.strftime

In [340]: print(current_time.strftime('%m/%d/%Y')) Out[340]: '08/05/2017' 
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, I forgot that print required brackets. Too much time on code academy and not using the editor slipped me up on that.
If you don't want the month or day to have leading zeros then on Linux/Unix use %-m/%-d/%Y. For Windows use %#m/%#d/%Y .

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.