有时编写程序,可能多次用到print函数,软件中程序如果不加特别声明,每条print语句会换行输出。那么python如何打印不换行呢?一起来了解下吧:
python如何打印不换行
[图片0]
当我们想要打印一句话后,希望用户输入相应信息,但是不希望,这句话打印后,光标换行闪动。
比如:
print "how old are you? please input your age:"
age = raw_input()
我们发现这时候光标是换行闪动的。
也就是说print 打印完后,是自动打印换行符的,那么如何避免光标换行闪动呢?
很简单,我们只需要这样改:
print "how old are you? please input your age:",
age = raw_input()
在print后面跟上一个,号,print就不会输出换行符结束这一行了。
这个小技巧,在很多应用中都会用到,这样使得用户更清晰的知道自己应该输入什么。
Python print如何 输出不换行
方法一
print "123",
print "456"
优点:方便
缺点:中间有间隔
方法二
[图片1]
#文件首行或第二行
1:from __future__ import print_function
print('', end='')
python 3怎么打印不换行
>>> help(print)
Help on built-in function print in module builtins:
print(...)
print(value, ..., sep=' ', end='n', file=, flush=False)
Prints the values to a stream, or to by default.
Optional keyword arguments:
file: a file-like object (stream); defaults to the current .
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 = 'n'是换行,所以
print('I am ', end = ' ')
print('a beautilful girl')
就是 I am a beautiful girl了,哈哈
如何用python输出不换行
有两种方法
1.
import sys
('.')
2.
from __future__ import print_function
print('.', end='')