I want to combine this 2 strings and make a "path" for object800 to print the value
x = 'object'
y = '800'
object800 = 1000
a = (x + y)
print(int(a)) # i want to print 1000
I want to combine this 2 strings and make a "path" for object800 to print the value
x = 'object'
y = '800'
object800 = 1000
a = (x + y)
print(int(a)) # i want to print 1000
You should use globals
:
x = 'object'
y = '800'
object800 = 1000
print(globals()[x + y])
# Outputs 1000
Note that this won't work inside a function, for example:
x = 'object'
y = '800'
def func():
object800 = 1000
print(globals()[x + y])
# Raises KeyError: 'object800'
func()
If that's the case, you should use a different approach:
x = 'object'
y = '800'
def func():
object800 = 1000
varname = x + y
print(globals()[varname] if varname in globals() else locals()[varname])
# Outputs 1000
func()
You should use eval
.
x = 'object'
y = '800'
object800 = 1000
a = eval(x + y)
print(a)
Read more here: https://www.w3schools.com/python/ref_func_eval.asp