Reputation: 41
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
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
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"))
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