""" ^ SyntaxError: invalid syntax

Based on the example here, I tried to run the following code snippet but failed:

run_mode, num_repeats, num_runs = 'short', 5, 1000

s = f"""
{'-'*40}
# Operator Micro-benchmarks
# Run_mode: {run_mode}
# Num_repeats: {num_repeats}
# Num_runs: {num_runs}

{'-'*40}
"""

print(s)

The error is as follows:

$ python test.py 
  File "test.py", line 11
    """
      ^
SyntaxError: invalid syntax

I’m surprised at this error message because I don’t see anything wrong with my code. Any tips will be appreciated.

Regards,
Zhao

I ran the same code on colab, it works.
It might be a python version issue.

Works fine for me.

What version of Python are you using? I can only duplicate that error in Python 2.7. That is no longer supported and is extremely out of date. You should be using Python 3.10.

2 Likes

It works in CPython 3.6 and up:

$ pythons --file /tmp/foo
below cmd output started 2022 Sat Sep 24 09:40:41 AM PDT
/usr/local/cpython-0.9/bin/python (unknown) bad  
    Parsing error: file /tmp/foo, line 5:
    s = f"""
          ^
    Unhandled exception: run-time error: syntax error
/usr/local/cpython-1.0/bin/python (1.0.1) bad  
      File "/tmp/foo", line 5
        s = f"""
              ^
    SyntaxError: invalid syntax
/usr/local/cpython-1.1/bin/python (1.1) bad  
      File "/tmp/foo", line 13
        """
          ^
    SyntaxError: invalid syntax
/usr/local/cpython-1.2/bin/python (1.2) bad  
      File "/tmp/foo", line 13
        """
          ^
    SyntaxError: invalid syntax
/usr/local/cpython-1.3/bin/python (1.3) bad  
      File "/tmp/foo", line 13
        """
          ^
    SyntaxError: invalid syntax
/usr/local/cpython-1.4/bin/python (1.4) bad  
      File "/tmp/foo", line 13
        """
          ^
    SyntaxError: invalid syntax
/usr/local/cpython-1.5/bin/python (1.5.2) bad  
      File "/tmp/foo", line 13
        """
          ^
    SyntaxError: invalid syntax
/usr/local/cpython-1.6/bin/python (1.6.1) bad  
      File "/tmp/foo", line 13
        """
          ^
    SyntaxError: invalid syntax
/usr/local/cpython-2.0/bin/python (2.0.1) bad  
      File "/tmp/foo", line 13
        """
          ^
    SyntaxError: invalid syntax
/usr/local/cpython-2.1/bin/python (2.1.0) bad  
      File "/tmp/foo", line 13
        """
          ^
    SyntaxError: invalid syntax
/usr/local/cpython-2.2/bin/python (2.2.0) bad  
      File "<string>", line 13
        """
          ^
    SyntaxError: invalid syntax
/usr/local/cpython-2.3/bin/python (2.3.0) bad  
      File "/tmp/foo", line 13
        """
          ^
    SyntaxError: invalid syntax
/usr/local/cpython-2.4/bin/python (2.4.0) bad  
      File "/tmp/foo", line 13
        """
          ^
    SyntaxError: invalid syntax
/usr/local/cpython-2.5/bin/python (2.5.6) bad  
      File "/tmp/foo", line 13
        """
          ^
    SyntaxError: invalid syntax
/usr/local/cpython-2.6/bin/python (2.6.9) bad  
      File "/tmp/foo", line 13
        """
          ^
    SyntaxError: invalid syntax
/usr/local/cpython-2.7/bin/python (2.7.16) bad  
      File "/tmp/foo", line 13
        """
          ^
    SyntaxError: invalid syntax
/usr/local/cpython-3.0/bin/python (3.0.1) bad  
      File "/tmp/foo", line 13
        """
          ^
    SyntaxError: invalid syntax
/usr/local/cpython-3.1/bin/python (3.1.5) bad  
      File "/tmp/foo", line 13
        """
          ^
    SyntaxError: invalid syntax
/usr/local/cpython-3.2/bin/python (3.2.5) bad  
      File "/tmp/foo", line 13
        """
          ^
    SyntaxError: invalid syntax
/usr/local/cpython-3.3/bin/python (3.3.7) bad  
      File "/tmp/foo", line 13
        """
          ^
    SyntaxError: invalid syntax
/usr/local/cpython-3.4/bin/python (3.4.8) bad  
      File "/tmp/foo", line 13
        """
          ^
    SyntaxError: invalid syntax
/usr/local/cpython-3.5/bin/python (3.5.5) bad  
      File "/tmp/foo", line 13
        """
          ^
    SyntaxError: invalid syntax
/usr/local/cpython-3.6/bin/python (3.6.13) good 
    
    ----------------------------------------
    # Operator Micro-benchmarks
    # Run_mode: short
    # Num_repeats: 5
    # Num_runs: 1000
    
    ----------------------------------------
/usr/local/cpython-3.7/bin/python (3.7.0) good 
    
    ----------------------------------------
    # Operator Micro-benchmarks
    # Run_mode: short
    # Num_repeats: 5
    # Num_runs: 1000
    
    ----------------------------------------
/usr/local/cpython-3.8/bin/python (3.8.0) good 
    
    ----------------------------------------
    # Operator Micro-benchmarks
    # Run_mode: short
    # Num_repeats: 5
    # Num_runs: 1000
    
    ----------------------------------------
/usr/local/cpython-3.9/bin/python (3.9.0) good 
    
    ----------------------------------------
    # Operator Micro-benchmarks
    # Run_mode: short
    # Num_repeats: 5
    # Num_runs: 1000
    
    ----------------------------------------
/usr/local/cpython-3.10/bin/python (3.10.0) good 
    
    ----------------------------------------
    # Operator Micro-benchmarks
    # Run_mode: short
    # Num_repeats: 5
    # Num_runs: 1000
    
    ----------------------------------------
/usr/local/pypy3-7.3.9/bin/pypy3 (3.9.12) good 
    
    ----------------------------------------
    # Operator Micro-benchmarks
    # Run_mode: short
    # Num_repeats: 5
    # Num_runs: 1000
    
    ----------------------------------------

22 bad  ************************************************************
 6 good ****************

Unsurprising, f-strings were added in version 3.6.

2 Likes

Hongyi, please include python version with questions.