如何防止 Python 打印添加换行符或空格? [英] How do I keep Python print from adding newlines or spaces?
本文介绍了如何防止 Python 打印添加换行符或空格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在python中,如果我说
In python, if I say
print 'h'
我得到了字母 h 和一个换行符.如果我说
I get the letter h and a newline. If I say
print 'h',
我得到了字母 h 而没有换行符.如果我说
I get the letter h and no newline. If I say
print 'h',
print 'm',
我得到了字母 h、一个空格和字母 m.如何防止 Python 打印空格?
I get the letter h, a space, and the letter m. How can I prevent Python from printing the space?
print 语句是同一个循环的不同迭代,所以我不能只使用 + 运算符.
The print statements are different iterations of the same loop so I can't just use the + operator.
推荐答案
import sys
sys.stdout.write('h')
sys.stdout.flush()
sys.stdout.write('m')
sys.stdout.flush()
你需要调用sys.stdout.flush()
因为否则它会将文本保存在缓冲区中,而您将看不到它.
You need to call sys.stdout.flush()
because otherwise it will hold the text in a buffer and you won't see it.
这篇关于如何防止 Python 打印添加换行符或空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文