Raj Pathak
Raj Pathak

Reputation: 41

how to print it in next line in python

import datetime 
currentTime = datetime.datetime.now() 
print ("Current date and time is:", currentTime.strftime("%Y-%m-%d %H:%M:%S"))

Output is:

Current date and time is: 2016-10-18 22:31:21

I want it to be:- without using another print statement may be /n which is not working:-

Current date and time is: 2016-10-18 22:31:21

Help?

Upvotes: 1

Views: 2466

Answers (3)

Padraic Cunningham
Padraic Cunningham

Reputation: 180391

You can pass whatever separator you like with the sep keyword sep="\n":

 In [1]: import datetime
   ...: currentTime = datetime.datetime.now()
   ...: print ("Current date and time is:", currentTime.strftime("%Y-%m-%d %H:%M
   ...: :%S"),sep="\n")
   ...: 
Current date and time is:
2016-10-18 18:21:25

If you wanted multiple newlines:

In [4]: print ("Current date and time is:", currentTime.strftime("%Y-%m-%d %H:%M
   ...: :%S"), sep="\n" * 3)
Current date and time is:


2016-10-18 18:21:25

Upvotes: 2

elethan
elethan

Reputation: 16993

Add a newline character ('\n') to your print:

print("Current date and time is:\n", currentTime.strftime("%Y-%m-%d %H:%M:%S"))

EDIT

As @StevenRumbalski points out, it would be better to use string concatenation with +:

print("Current date and time is:\n" + currentTime.strftime("%Y-%m-%d %H:%M:%S"))

to avoid the indentation that results from my previous code.

Upvotes: 4

Patrick Haugh
Patrick Haugh

Reputation: 60944

Newline character is \n not /n

Upvotes: 0

Related Questions