-2

I'm trying to use **kwargs and facing a syntax error while using f-string in print function. I have no idea why it that an error? I'm using Python3.7.4 .

def func(**kwargs):
    for k, v in kwargs.items():
        print(f' {k} : {v} ')

func(first_name = 'x', last_name = 'y')

File "130819.py", line 4
    print(f' {k} : {v} ')
                       ^

SyntaxError: invalid syntax

Ramprasath Selvam
  • 3,868
  • 3
  • 25
  • 41
ankur29mac
  • 19
  • 2
  • 7
  • Your code works fine: https://ideone.com/Ci1woh. Make sure you copied the correct code and check your python version you're using to run this script – awesoon Aug 13 '19 at 07:15
  • please check the version of python , as f-string are not present in python2 ... – Mukul Kumar Jha Aug 13 '19 at 07:15
  • This code works perfectly on 3.6.8 and I can't see why it wouldn't on 3.7.4. Are you sure you're not using 2.7.4 instead ??? – bruno desthuilliers Aug 13 '19 at 07:18
  • I can also confirm that this works fine in both Python 3.6.8 and 3.7.3. – itroulli Aug 13 '19 at 07:20
  • I am using Linux and I have 3 different Python versions installed on the system. By default, I have Python2 set as an interpreter. Using Python3 as the command solved the syntax error, Thanks y'all. – ankur29mac Aug 13 '19 at 07:29
  • Possible duplicate of [f-strings giving SyntaxError?](https://stackoverflow.com/questions/50401632/f-strings-giving-syntaxerror) – Thierry Lathuille Aug 13 '19 at 08:10

1 Answers1

1

Your Code works fine with python 3.7 and gives the expected output

 def func(**kwargs):
...     for k, v in kwargs.items():
...         print(f' {k} : {v} ')
... 
 func(first_name = 'x', last_name = 'y')

output is

 first_name : x 
 last_name : y 

I think the problem is with your python version , as f-string literals are present only in python 3.6 +

Try to check the version with python --version

Mukul Kumar Jha
  • 1,062
  • 7
  • 19