如何在Python中将变量放入此字符串?

时间:2014-07-24 12:26:55

标签: python variables

我差不多有这行代码 -

Popen(["C:\Users\Johannes\Documents\A Customized JonDoFox Portable Profiles - Beta 2\JonDoFoxPortable1\JonDoFoxPortable.exe", "https://www.amazon.com/gp/sign-in.html"], )

但是我需要在JonDoFoxPortable(VariableHere)中插入一个变量,所以它看起来像那样 -

Popen(["C:\Users\Johannes\Documents\A Customized JonDoFox Portable Profiles - Beta 2\JonDoFoxPortable(VariableHere)\JonDoFoxPortable.exe", "https://www.amazon.com/gp/sign-in.html"], )

仍然是初学者,但如果有人有任何想法,我将感激不尽:)

2 个答案:

答案 0 :(得分:1)

您可以使用format功能。

>>> txt = "MyText{}"
>>> txt.format(2)
'MyText2'

答案 1 :(得分:1)

有多种方式:

Popen(["C:\Users\Johannes\Documents\A Customized JonDoFox Portable Profiles - Beta 2\JonDoFoxPortable" + variable + "\JonDoFoxPortable.exe", "https://www.amazon.com/gp/sign-in.html"], )

Popen(["C:\Users\Johannes\Documents\A Customized JonDoFox Portable Profiles - Beta 2\JonDoFoxPortable%a\JonDoFoxPortable.exe"%variable, "https://www.amazon.com/gp/sign-in.html"], )

或者Sukrit Kalra发布的内容