简体   繁体   中英

How to print combined var name in python 3.7

I would like to print new_firstsecond's value, without knowing it's name in advance, how can I do this in python 3.7?

var1 = "first"
var2 = "second"

new_firstsecond = "some value"

print("new_"+f"{vars()[var1]}"+f"{vars()[var2]}")

You can do something like this,

var1 = "first"
var2 = "second"

new_firstsecond = "some value"

for variable, value in globals().copy().items():
    print(variable, value)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM