assign output of print to a variable in python

前端 未结 6 1423
心在旅途
心在旅途 2021-01-15 01:41

i want to know how to assign the output of print to a variable.

so if

mystring = \"a=\\\'12\\\'\"

then

print mys         


        
6条回答
  •  失恋的感觉
    2021-01-15 02:12

    If you have a string 'my_string' like this:

    a=123 b=456 c='hello'
    

    Then you can pass it to a function 'my_fun' like this:

    my_fun(**eval('{' + my_string.replace(' ', ',') + '}'))
    

    Depending on the precise formatting of my_string, you may have to vary this a little, but this should get you 90% of the way there.

提交回复
热议问题