**kwargs in Python

def greet_me(**kwargs):
    for key, value in kwargs.items():
        print("{0} = {1}".format(key, value))

greet_me(name = 'Aakash')

Here why can’t I put ‘name’ like in this quotation.
Cause when I do this it gives me an error:
keyword can’t be an expression
(I tried searching on google about keyword can’t be an expression but I still have a bit doubt)

Also, I don’t understand why to do this : print("{0} = {1}".format(key, value))
Instead, I can do it like this: print(key +’ =’,value)
My doubt is what’s that 0 and 1 in the print value cause I didn’t stumble upon anything like this on LPTHW.

I don’t get an error if I run your code exactly like in your example.

name is an identifier and can be used on the left side of assignment statements, and as a keyword for your function.

'name' is a string literal: a value, it can only be used on the right side of assignment statements.

You can do name = 'Aakash', but 'name' = 'Aakash' doesn’t make sense to the Python interpreter because there are values on both sides of the assignment operator.

As for the string formatting: Maybe take a look at the section about string formatting in the official documentation. 0 and 1 just refer to the indexes of the arguments. If they are used in the correct order you could leave the numbers out.

There are many ways to print out these two values. You are right that you could just do print(key, "=", value). I think in LPTHW Zed teaches you to use f strings: print(f"{key} = {value}").

1 Like

Like @florian says, your code seems to run fine for me. I think your error may either be in another part of the code that you didn’t post, or that was an error in it which you fixed, but did not save.

1 Like