你如何在python的变量旁边放一个句点?

kin*_*ing 0 python

我是python的新手,所以对我们来说这很容易,我敢肯定.

我在python中写了一个简单的东西,但我很好奇如何在我正在使用的变量旁边放一个句点,我会告诉你:

i = 5
print "i is equal to", i,"."
Run Code Online (Sandbox Code Playgroud)

我运行它,它出来像这样:

i is equal to 5 .
Run Code Online (Sandbox Code Playgroud)

你如何做到这一点(句号)就在5号旁边,就像"我等于5".

我试着寻找这个,但我觉得它太简单了,我找不到它大声笑,我不知道究竟是什么东西,谢谢.

bra*_*ers 7

使用字符串格式:

print "i is equal to %d." % i
Run Code Online (Sandbox Code Playgroud)

从Python 2.6开始,字符串有一个.format()可以替代使用的函数:

>>> print "i is equal to {}".format(i)
i is equal to 5
Run Code Online (Sandbox Code Playgroud)

如果你有一个在格式字符串中多次使用的占位符,它会很有用:

>>> print "i={0}, a.k.a. 'i is equal to {0}.'".format(i)
i=5, a.k.a. 'i is equal to 5.'
Run Code Online (Sandbox Code Playgroud)


Yun*_* Ma 7

你也可以像这样连接,

print "i is equal to " + str(i) + "."
Run Code Online (Sandbox Code Playgroud)