擅长:python、mysql、java
<p>您遇到的问题是由运算符优先级引起的。你知道吗</p>
<p>下一行之所以有效是因为它是<a href="https://docs.python.org/2.0/ref/string-catenation.html" rel="nofollow">string literal concatenation</a>,它的优先级高于<code>%</code>运算符。你知道吗</p>
<pre><code>print "%s" "/" "%s" "/" "%s" % (now.month, now.day, now.year)
</code></pre>
<p>以下操作不起作用,因为<code>+</code>运算符的优先级低于<code>%</code>运算符。你知道吗</p>
<pre><code>print "%s" + "/" + "%s" + "/" + "%s" % (now.month, now.day, now.year)
</code></pre>
<p>要修复它,请在串联中添加括号,以便首先执行它,如下所示:</p>
<pre><code>print ("%s" + "/" + "%s" + "/" + "%s") % (now.month, now.day, now.year)
</code></pre>