- 原理
Python的print()函数中参数end='' 默认为\n,所以会自动换行;
默认的print()函数: print(end='\n')
- 方案
Python 2: 在print语句的末尾加上一个逗号, 如
print "Hello World",
Python 3: 把参数end
设置成你想要的就行了, 如print("Hello World", end="")
- 扩展
补充:其实print()有两个比较重要的可选参数,一个是
end
一个是sep
print()打印中支持常用制表符, 如
\t
,\n
end 在上面已经有介绍了,下面说一下sep,看示例就可以知道具体的意思了: print('cats', 'dogs', 'mice') 输出: cats dogs mice print('cats', 'dogs', 'mice', sep = ',') 输出: cats,dogs,mice 上述就是用的','替换掉了分隔符 ,当然你也可以用于替换成其他你想要的符号,这个功能有时候会比较有用 譬如2019-10-01是我们祖国70周年 print('2019','10','01', sep='-') 输出: 2019-10-01