如何保持Python打印不添加换行符或空格? [英] How do I keep Python print from adding newlines or spaces?

查看:532
本文介绍了如何保持Python打印不添加换行符或空格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在python中,如果我说
$ b $ pre $ print'h'

pre>

我得到字母h和一个换行符。如果我说

$ p $ print'h',

我得到字母h,没有换行符。如果我说

  print'h',
print'm',

我得到字母h,空格和字母m。如何防止Python打印空间?



打印语句是同一个循环的不同迭代,所以我不能只使用+运算符。

解决方案

您可以使用:

  sys .stdout.write('h')
sys.stdout.write('m')


In python, if I say

print 'h'

I get the letter h and a newline. If I say

print 'h',

I get the letter h and no newline. If I say

print 'h',
print 'm',

I get the letter h, a space, and the letter m. How can I prevent Python from printing the space?

The print statements are different iterations of the same loop so I can't just use the + operator.

解决方案

You can use:

sys.stdout.write('h')
sys.stdout.write('m')

这篇关于如何保持Python打印不添加换行符或空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆