Python 3.x下消除print()自动换行
2018-01-16 22:50
288 查看
Python 2.x下的print语句在输出字符串之后会默认换行,如果不希望换行,只要在语句最后加一个“,”即可。但是在Python 3.x下,print()变成内置函数,加“,”的老方法就行不通了。
查询Python的Library Reference>Built-in Functions,找到如下条目:
“print([object, ...], *, sep='
', end='\n', file=sys.stdout)
Print object(s) to the stream file, separated by sep and
followed by end. sep, endand file, if present, must be given as keyword arguments.
All non-keyword arguments are converted to strings like str() does
and written to the stream, separated by sep and followed by end. Both sep and end must
be strings; they can also be None, which means to use the default values. If no objectis
given, print() will
just write end.
The file argument must be an object with a write(string) method;
if it is not present or None, sys.stdout will
be used. ”
其中,sep=''和end='\n'均是print()的关键参数,sep的默认值为空,end默认值为换行符,这就是print()在输出后默认换行的原因。相应的,解决办法就是对end赋值:print(something, something,.., end=''),使end值为空,这个换行就消除了。
原文地址:http://wpp9977777.blog.163.com/blog/static/46251007201192905622402/
查询Python的Library Reference>Built-in Functions,找到如下条目:
“print([object, ...], *, sep='
', end='\n', file=sys.stdout)
Print object(s) to the stream file, separated by sep and
followed by end. sep, endand file, if present, must be given as keyword arguments.
All non-keyword arguments are converted to strings like str() does
and written to the stream, separated by sep and followed by end. Both sep and end must
be strings; they can also be None, which means to use the default values. If no objectis
given, print() will
just write end.
The file argument must be an object with a write(string) method;
if it is not present or None, sys.stdout will
be used. ”
其中,sep=''和end='\n'均是print()的关键参数,sep的默认值为空,end默认值为换行符,这就是print()在输出后默认换行的原因。相应的,解决办法就是对end赋值:print(something, something,.., end=''),使end值为空,这个换行就消除了。
原文地址:http://wpp9977777.blog.163.com/blog/static/46251007201192905622402/
相关文章推荐
- Python 3.x下消除print()自动换行
- Python: 消除print的自动换行
- python print 自动换行
- python print输出不自动换行
- 用Python编写九九乘法表考虑print自动换行问题
- Python小工具——自动去除剪贴板中的换行
- python写入文件自动换行问题
- 零基础学python-在3.x版本之后的print()不换行
- 零基础学python-在3.x版本之后的print()不换行
- python中print不换行的方法
- Python print 不换行的方法
- 关于python写入文件自动换行的问题
- python print实现不换行打印
- python3.x中如何实现print不换行 end=' '
- python print不换行(更改print的默认输出格式)
- Python print 不换行打印
- python3中如何去掉print打印的换行
- print不自动换行,puts会自动换行第1/2页
- python3.x之print()不换行解决方案
- python消除print的换行符、引用模块和异常处理