2

I'm trying to follow this easy Python exercise but I can't achieve it in a Python shell and would like an explanation why. Below is the solution to a Python question which you can put in a compiler, but if I wanted to enter these in a shell, why doesn't adding a back slash allow me to continue writing lines? I get invalid syntax trying to write the following. Of course, I can't even make it beyond the second line without a syntax error. My shell is Python 3.8.9.

import datetime \
now = datetime.datetime.now() \
print ("Current date and time : ") \
print (now.strftime("%Y-%m-%d %H:%M:%S"))
1
  • 8
    enter one line at a time and press return, without the slashes. why do you think adding a slash should let you add more lines? Commented Jun 14, 2022 at 16:21

4 Answers 4

4

It doesn't seem to be as widely known, but Python will treat ; much like some other languages.

For example, I can put the following into my shell/terminal:

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

And the output will be:

Current date and time :
2022-06-14 09:26:37

That being said, I doubt this is considered good practice and you'd typically be better off just entering in commands line by line. Without using ; to put multiple commands in a single line.

In most shell/terminals, if you just copy/paste the following into them:

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

The result will look something like:

>>> import datetime
>>> now = datetime.datetime.now()
>>> print ("Current date and time : ")
Current date and time :
>>> print (now.strftime("%Y-%m-%d %H:%M:%S"))
2022-06-14 09:37:30

Which may not be exactly what you're hoping for, but the shell/terminal isn't exactly meant to look 'pretty'...

Sign up to request clarification or add additional context in comments.

Comments

2

A backslash (\) lets you to break a single line into multiple lines; It is not meant to separate multiple statements or expressions. What I mean is:

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

is equivalent to;

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

So the simple solution is to remove the backslashes as you don't need to write everything in a single line.

Comments

2

With python3.8.1 on Ubuntu 20.04.4 LTS, this works for me:

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

Output:

Python 3.8.10 (default, Mar 15 2022, 12:22:08) 
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import datetime;\
... now = datetime.datetime.now();\
... print("Current date and time : ");\
... print(now.strftime("%Y:%m:%d %H:%M:%S"))
Current date and time : 
2022:06:14 13:38:44
>>> 

1 Comment

Works fine in 3.9 for me as well, although I hate it :')
0

Oh! Apparently you can separate everything with a semicolon:

import datetime ; now= datetime.datetime.now() ; print("Current date and time: ") ; print(now.strftime("%Y-%m-%d %H:%M:%S"))
Current date and time: 
2022-06-14 09:38:07

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.