2015-10-17 66 views
1

如何在“”中打印变量?例如, ;如何在“”内打印变量?

a = "test" 
print"this is a (how do I input 'a' here?)" 
+0

使用'打印 “这是(我怎么输入 '%s' 的吗?)” %了' –

+0

@TamilSelvan运算使用Python 3.x的 –

+0

@AvinashRaj @idjaw你是对的。 python 3有不同的语法。尝试'print(“这是一个(我怎么输入”,a,“在这里?)”) –

回答

4

你应该阅读打印tutorial

但你在找什么做的是这样的:

print("this is a {} here?".format(a)) 

如果您正在寻找把多个变量在你的字符串,你可以简单地做到这一点:

print("this is a {0} here and {1} and also {2}?".format(a, b, c)) 

你实际上可以写这样的线这样witho UT斯达康数量规格:

print("this is a {} here and {} and also {}?".format(a, b, c)) 

作为明确与数字将是最好的,如果你正在寻找重新排序的参数放置在字符串中,像这样的:

print("this is a {2} here and {0} and also {1}?".format(a, b, c)) 

所以,上面的例子中,第一个字符串是c,然后是a,然后是b。

对于您试图在字符串中添加引号的特定情况,可以采用不同的方法来执行此操作。如果你想双引号,你可以用单引号包:

print('this is a "{2}" here and "{0}" and also "{1}"?'.format(a, b, c)) 

如果你想单引号,换用双引号:

print("this is a '{2}' here and '{0}' and also '{1}'?".format(a, b, c)) 

最后,还有三单引号:

print('''this is a '{2}' here and '{0}' and also '{1}'?'''.format(a, b, c)) 

混合取其类型的报价:

print('''this is a "{2}" here and "{0}" and also '{1}'?'''.format(a, b, c)) 
+0

除非重新排序用法,或者多次重复使用同一个参数,否则不需要显式编号。第二个例子也可以是三个用法。 – ShadowRanger

+0

是的,这是正确的。为了这个例子,我只是想更加明确。我会记下这个艰难的。谢谢。 – idjaw