How does the function turn `**kwargs` from variables into strings?

Hello. I have a huge question about kwarg.
Cuz in this problem’s answer, key value of kwargs are not string but how can it be change to string?

def remove(filename, *args, **kwargs):
  with open(filename) as file_obj:
    text = file_obj.read()
  for arg in args:
    text = text.replace(arg, "")
  for kwarg, replacement in kwargs.items():
    text = text.replace(kwarg, replacement)
  return text

print(remove("text.txt", "generous", "gallant", fond="amused by", Robin="Mr. Robin"))

Hi, @asiadecoder,

To rephrase your question, you are wondering why the arguments passed in for **kwargs parameter appear to be variables (rather than strings) and yet the remove function’s text = text.replace(kwarg, replacement) operation still treats the keyword as a string. This is explained by the fact that the unpacked keyword arguments passed to the function are stored by the function as a dictionary (as opposed to unpacked positional arguments which are stored by the function as a tuple). This is illustrated by the example given in the lesson:

A possible call to the function could look like this:

main(“files/users/userslist.txt”,
“-d”,
“-a”,
save_all_records=True,
user_list=current_users)
In the body of main() these arguments would look like this:

filename == “files/users/userslist.txt”
args == (’-d’, '-a)
user_list == current_users
kwargs == {‘save_all_records’: True}

Notice at the end the dictionary object has stored the passed arugment save_all_records = True as ‘save_all_records’: True.

It would appear that the purpose of utilizing unpacked keyword arguments is to allow individual key/value pairs to be passed to the function rather than requiring that a dictionary object be passed into the function. You can find more details in the Phython documentation here.