Python: 消除print的自动换行
来源:互联网 发布:淘宝无线套餐链接转换 编辑:程序博客网 时间:2024/06/09 13:46
Python: 消除print换行
标签:python print \n
by 小威威
对于python2.X,要消除print的自动换行,只需在print尾部加上一个逗号”,”,但是这一做法在python3.X就不适用了,这是为什么呢?我们可以在交互式的环境下输入help(print),查询print的原理和使用方法。
Help on built-in function print in module builtins:
print(…)
print(value, …, sep=’ ‘, end=’\n’, file=sys.stdout, flush=False)
appledeMacBook-Pro-2:Desktop apple$ python3Python 3.5.0 (v3.5.0:374f501f4567, Sep 12 2015, 11:00:19) [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwinType "help", "copyright", "credits" or "license" for more information.>>> help(print)Help on built-in function print in module builtins:print(...) print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False) Prints the values to a stream, or to sys.stdout by default. Optional keyword arguments: file: a file-like object (stream); defaults to the current sys.stdout. sep: string inserted between values, default a space. end: string appended after the last value, default a newline. flush: whether to forcibly flush the stream.(END)
注意看这一句:
print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
这一句说明在python3中print是一个函数,对于函数,其形参中有默认参数和关键参数。我们发现,在结尾处出现了end = ‘\n’,说明print是以\n结束的,end是默认参数。只要我们在print中将默认参数的值改为空或者空格,就能实现不换行。
举个栗子:
#!/usr/bin/python3# Filename: using_list.py# This is my shopping listshoplist = ['apple', 'mango', 'carrot', 'banana']print ('These items are:')for i in shoplist: print (i,end=' ')# End
输出结果如下:
These items are:apple mango carrot banana
以上内容皆为本人观点,欢迎大家提出批评和指导,我们一起探讨。
0 0
- Python: 消除print的自动换行
- python print 自动换行
- python消除print的换行符、引用模块和异常处理
- python print输出不自动换行
- 防止 print 自动换行
- python中print不换行的方法
- Python print 不换行的方法
- Python print 不换行打印
- Python print 不换行打印
- Python print打印不换行
- Python print 不换行打印
- python中print不换行
- Python print 输出不换行
- Python Print 打印不换行
- Python: print 不换行打印
- python中print的不换行即时输出解决方案
- python中print的不换行即时输出解决方案
- python print不换行(更改print的默认输出格式)
- R语言里调用C++
- JS组件系列——Bootstrap寒冬暖身篇:弹出框和提示框效果以及代码展示
- HDU 2955 Robberies
- linux那些命令
- 键盘输入缓存的清理
- Python: 消除print的自动换行
- 《剑指offer》——二维数组中的查找
- 【查找算法】——顺序查找、折半查找、分块查找(索引查找)
- Light OJ 1377 - Blade and Sword (BFS)
- 杭电2037-今年暑假不AC(贪心)
- Maven隐士变量
- 关于viewController中的tableview的cell在pop回来后仍有选中效果的解决方法
- 3:输入一个整数,求这个数的阶乘
- C# 多个Main()方法