让python输出不自行换行的方法

时间:2015-02-01 20:17:19   收藏:0   阅读:191

1,在输出内容后加逗号

例:

for i in range(1,6):
    j = 1
    while(j <= 2*i - 1):
        print "*",
        j = j + 1
    print "\n"

输出结果为:

*
* * *
* * * * *
* * * * * * *
* * * * * * * * *

但每个*之间有个空格

 

2.导入sys,利用sys.stdout.write()来进行输出

例:

import sys
for i in range(1,6):
    j = 1
    while(j <= 2*i - 1):
        sys.stdout.write("*")
        j = j + 1
    print "\n"

输出结果为:

*
***
*****
*******
*********

 

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!