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

后端 未结 5 1753
傲寒
傲寒 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 08:17

    def get_arg(yourstring):
        arg_list = []
        pin = False
        for ch in yourstring:
            if ch == "{":
                pin = True
            elif ch == "}":
                pin = False
                arg_list.append(text)
                text = ""
            elif pin:
                text +=ch
        return arg_list
    

提交回复
热议问题