user2573593
user2573593

Reputation: 21

Python printing

I downloaded python 3.3. I am just learning python and wanted to try some of it out with the actual IDE. I want to print the date and time. It says syntax error when I type in print _ . Please check if there is something wrong with the code or syntax :

>>> from datetime import datetime
>>> now = datetime.now()
>>> print now
SyntaxError: invalid syntax


>>> from datetime import datetime

>>> current_year = now.year

>>> current_month = now.month

>>> current_day = now.day

>>> print now.month

SyntaxError: invalid syntax

>>> print now.day

SyntaxError: invalid syntax

>>> print now.year

SyntaxError: invalid syntax

Upvotes: 0

Views: 153

Answers (2)

Stephan
Stephan

Reputation: 17971

in python 2.x you used print like this

print "hi"

in python 3.x the print statement was upgraded to a function to allow it to do more things and behave more predictably, like this

print("hi")

Or more specifically, in your case: print(now.year)

With the new print function you can do all sorts of things like specifying the terminating character, or printing straight to a file

print("hi" end = ",")
print("add this to that file", file=my_file)

You can also do things that you couldn't have done with the old statment, such as

[print(x) for x in range(10)]

Upvotes: 2

Daniel Roseman
Daniel Roseman

Reputation: 599450

print is a function in Python 3.

 >>> print(now.year)

Upvotes: 4

Related Questions