使用以下代碼:x = "tuple of strings: {}"y = ("hello", "world")print(x.format(y))我得到輸出:tuple of strings: ('hello', 'world')相反,我想獲得輸出:tuple of strings: (hello, world)這個怎么做?謝謝
4 回答

絕地無雙
TA貢獻1946條經驗 獲得超4個贊
您可以從格式化字符串中手動刪除 ':
x = "tuple of strings: {}"
y = ("hello", "world")
print(x.format(y).replace("'",''))
輸出:
tuple of strings: (hello, world)

千萬里不及你
TA貢獻1784條經驗 獲得超9個贊
正如其他人所提供的,如果它們不是對象,那是不可能的。
如果他們是那么:
這將輸出對象(如hello
):
# str will be like : 'hello' globals()[str]
或者
eval(str)
筆記 :
但我建議創建一個字典,其中鍵作為字符串,值作為相應的數組,而不是eval
和globals()
像這樣:
dict_ = {'hello': hello, 'world': world}
并在任何你想訪問變量的地方使用這個字典
添加回答
舉報
0/150
提交
取消