Skip to content Skip to sidebar Skip to footer

Syntaxerror When Using Literal String Interpolation Or F-strings

Trying to read a csv file and print contents: with open('C:\test.csv') as csvfile: csv_reader = csv.reader(csvfile, delimiter=',') line_count = 0 for row in csv_reader:

Solution 1:

Literal String Interpolation or "f-strings" was only introduced in Python3.6. See: https://www.python.org/dev/peps/pep-0498/

PEP498

Your code works fine on Python3.6. If you are not using Python3.6 (and up), you will get a syntax error.

$ python3.6 -V
Python 3.6.7
$ python3.6 readcsv.py 
Column names are c, o, l,11 works in the 2 department in3.4 works in the 5 department in6.

$ python3 -V
Python 3.5.2
$ python3 readcsv.py 
  File "readcsv.py", line 9print(f'Column names are {", ".join(row[0])}')
                                                ^
SyntaxError: invalid syntax

$ python -V
Python 2.7.12
$ python readcsv.py 
  File "readcsv.py", line 9print(f'Column names are {", ".join(row[0])}')
                                                ^
SyntaxError: invalid syntax

Post a Comment for "Syntaxerror When Using Literal String Interpolation Or F-strings"