Maison > Article > développement back-end > 怎样使 Python 输出时不换行?
教程上说用逗号就可以了。为什么我用了逗号还是没有用呢?我用的是python3.2 。在print后面要加圆括号。
print ('I'),
print ('love'),
print ('you.')
input ()
<code class="language-python"><span class="kn">import</span> <span class="nn">sys</span>
<span class="n">sys</span><span class="o">.</span><span class="n">stdout</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s">'.'</span><span class="p">)</span>
<span class="c"># 在 python 2.x 下用 python 3 的 print</span>
<span class="kn">from</span> <span class="nn">__future__</span> <span class="kn">import</span> <span class="n">print_function</span>
<span class="k">print</span><span class="p">(</span><span class="s">'.'</span><span class="p">,</span> <span class="n">end</span><span class="o">=</span><span class="s">''</span><span class="p">)</span>
<span class="c"># ps. 这种写法并不是正确的,会在末尾多一个空格</span>
<span class="k">print</span> <span class="s">'.'</span><span class="p">,</span>
</code>
<code class="language-python"><span class="k">print</span><span class="p">(</span><span class="o">*</span><span class="n">objects</span><span class="p">,</span> <span class="n">sep</span><span class="o">=</span><span class="s">' '</span><span class="p">,</span> <span class="n">end</span><span class="o">=</span><span class="s">'</span><span class="se">\n</span><span class="s">'</span><span class="p">,</span> <span class="nb">file</span><span class="o">=</span><span class="n">sys</span><span class="o">.</span><span class="n">stdout</span><span class="p">,</span> <span class="n">flush</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
</code>
在末尾加一个 逗号 就可以了。