在python中将字符串转换为变量名 [英] Convert string to variable name in python
本文介绍了在python中将字符串转换为变量名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有任何字符串.像'水牛',
I have any string. like 'buffalo',
x='buffalo'
我想将此字符串转换为某个变量名称,例如
I want to convert this string to some variable name like,
buffalo=4
不仅仅是这个例子,我想将任何输入字符串转换为某个变量名.我应该怎么做(在 python 中)?
not only this example, I want to convert any input string to some variable name. How should I do that (in python)?
推荐答案
x='buffalo'
exec("%s = %d" % (x,2))
之后,您可以通过以下方式进行检查:
After that you can check it by:
print buffalo
作为输出,您将看到:2
这篇关于在python中将字符串转换为变量名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文