Python print打印不换行
来源:互联网 发布:2016的网络用语 编辑:程序博客网 时间:2024/04/29 07:00
Python中的print默认是换行的
想要不换行输出有两种办法:
1.print后加上","
############################
>>>print "Hello World",
############################
2.使用sys.stdout.write命令
############################
>>>sys.stdout.write("Hello World")
############################
但是,以上的命令在具体执行时,并不会实时显示,每次都是在换行时才把整行指令打出来.
如果要实时显示,需要在每次sys.stdout.write后面加上一行sys.stdout.flush()让屏幕输出
############################
sys.stdout.write("Hello World")
sys.stdout.flush()
sys.stdout.write("one line!")
sys.stdout.flush()
############################
附一段我用来控制write换行情况的代码
############################
class ChangeLine:NewLine = True
@classmethod
def write_out(self,str,typename = 0):
# 0 is "\n.....\n"
if typename == 0:
if self.NewLine == False:
sys.stdout.write('\n')
sys.stdout.flush()
sys.stdout.write(str + '\n')
sys.stdout.flush()
self.NewLine = True
# 1 is "......."
if typename == 1:
sys.stdout.write(str)
sys.stdout.flush()
self.NewLine = False
# 2 is "\n......"
if typename == 2:
if self.NewLine == False:
sys.stdout.write('\n')
sys.stdout.flush()
sys.stdout.write(str)
sys.stdout.flush()
self.NewLine = False
# 3 is "......\n"
if typename == 3:
sys.stdout.write(str + '\n')
sys.stdout.flush()
self.NewLine = True
############################
0 0
- Python print 不换行打印
- Python print 不换行打印
- Python print打印不换行
- Python print 不换行打印
- Python Print 打印不换行
- Python: print 不换行打印
- Python print 输出打印不换行
- python中print不换行
- Python print 输出不换行
- Python 打印不换行
- python中print不换行的方法
- Python print 不换行的方法
- python print 不输出换行符
- python print输出不自动换行
- Python print输出不换行没空格
- python 基础知识-----print多次输入不换行
- python 3 打印不换行
- Swift的print不换行打印的方法
- reids 事务学习一
- 一个Python的小工具
- java开发常被问到的面试题-HashMap的底层原理
- SpringMVC中的参数组装:HandlerMethodArgumentResolver
- MySQL-Front 添加外键 约束
- Python print打印不换行
- 图的邻接表存储c实现
- 414. Third Maximum Number
- mysql事务
- 使用SIMD技术提高C++程序性能
- test
- Hololens Remoting player使用
- PAT1003我要通过!
- 标签与模板的应用