现在的位置: 首页 > 综合 > 正文

python string to variable

2012年12月15日 ⁄ 综合 ⁄ 共 201字 ⁄ 字号 评论关闭

example:

exec:执行字串语句

>>> l=[12,3,4,4]
>>> s = 'l.append(10)'
>>> exec(s)
>>> s
'l.append(10)'
>>> l
[12, 3, 4, 4, 10]
>>>

 

eval:让字串变成与之对应的变量名

>>> str = 'good'
>>> good = 'hello!world'
>>> eval(str)
'hello!world'
>>> good
'hello!world'

 

 

抱歉!评论已关闭.