python print实现不换行打印

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python print实现不换行打印相关的知识,希望对你有一定的参考价值。

一、环境以及背景

    winows10 + python2.7 + python3.5

     需要使用python2.7 中的print并且不换行


二、使用方法

   1. 在结尾添加一个逗号

        print "hello world",

    问题: 在输出结果后会多一个空格,这个空格把我坑了技术分享

    

   2. 使用sys模块

        import sys
     sys.stdout.write("hello world")    
    sys.stdout.flush()

    

    3. 使用python 3.x版本的print

       3.x版本 中的print函数增加了一个end参数, 默认值为end=‘\n‘

            print ("hello world", end=‘‘)


本文出自 “风雪舞者” 博客,请务必保留此出处http://happytree007.blog.51cto.com/6335296/1891902

以上是关于python print实现不换行打印的主要内容,如果未能解决你的问题,请参考以下文章