1

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
2
  • print("C://folder//" + str(datestr).replace("-", "_") + "//temp.csv") why is your code inside a string??? Your code won't work inside a string. Commented Jun 10, 2020 at 22:48
  • 1
    @High-Octane It is an f-string, so this will work once the problem with the string delimiters is fixed. Commented Jun 10, 2020 at 23:07

1 Answer 1

3

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!

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.