怎样使 Python 输出时不换行?

WBOY
Release: 2016-06-06 16:22:55
Original
1929 people have browsed it

教程上说用逗号就可以了。为什么我用了逗号还是没有用呢?我用的是python3.2 。在print后面要加圆括号。
print ('I'),
print ('love'),
print ('you.')
input ()

回复内容:

python 2.x, print 不换行
>>> print x,
python 3.x print 不换行
>>> print(x, end="")
<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>
Copy after login
<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>
Copy after login
在末尾加一个 逗号 就可以了。
等你输完了,你又想换行的时候,单独一个 print 就换行了。试试看。 直接写stdout或许是个装13的方案 print ('hello,',end=' ’)
print ('world!')

hello,world! lmgtfy.com/? print 'Hello'+' '+'World'
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template