简体   繁体   中英

Python formatting string invalid syntax

In python 3.7, I have this very simple script. Why is this throwing a invalid syntax error?

datestr = '2020-06-10'
print(f"C:/folder/{datestr.replace("-", "_")}/temp.csv")


 File "<ipython-input-38-95d22e47df04>", line 2
    print(f"C:/folder/{datestr.replace("-", "_")}/temp.csv")
                                             ^
SyntaxError: invalid syntax

You are trying to use " for different purposes in the same string. You can interchange it with ' to stop any confusion between string operations and terminating a string:

datestr = '2020-06-10'
print(f"C:/folder/{datestr.replace('-', '_')}/temp.csv")

Hope this helps!

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