How to get the variable names from the string for the format() method

后端 未结 5 1748
傲寒
傲寒 2020-12-18 07:32

Lets say I have this line:

\"My name is {name}\".format(name=\"qwerty\")

I know that the variable name is name and so I can f

5条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-18 07:53

    Or an alternative is to use a function:

    def format_string(name):
        return "My name is {name}".format(name="qwerty")
    

    Then call it:

    format_string("whatever")
    

提交回复
热议问题