简体   繁体   中英

SyntaxError: invalid syntax (when running python code from shell)

In Jupyter Notebook my Code runs fine, but when I run it from shell i get a syntax error at this part of my code:

res = f'"x"'+" "+f'"y"'+" "+f'"t"'+" "+f'"id"'+" "+f'"id2"'
           ^
SyntaxError: invalid syntax

However I need the String res to look like:

""x" "y" "t" "id" "id2""

I guess that the way i create it is causing the error.

Is there any other way to create a String containing quotation marks? Or anything to get rid of the syntax error? Thanks!

f-strings, or formatted string literals is supported only from python 3.6. If you are using old version of python, try to upgrade. If you have both python2 and python3 installed, make sure you are launching python3

But for the output you expect, you dont need f-strings

res = '"x" "y" "t" "id" "id2"'

Got the answer now:

res= "\"x\" \"y\" \"t\" \"id\" \"id2\""

works for Shell as well as Python

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM