python - 如何防止 Python 打印添加换行符或空格?

在python中,如果我说

print 'h'

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

print 'h',

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

print 'h',
print 'm',

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

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

最佳答案

在 Python 3 , 使用

print('h', end='')

抑制结束线终止符,并且

print('a', 'b', 'c', sep='')

抑制项目之间的空格分隔符。见 the documentation for print

https://stackoverflow.com/questions/255147/

相关文章:

bash - 如何在 Bash 中输出粗体文本?

windows - 在 Windows 批处理脚本中格式化日期和时间

android - 在Android中设置TextView span的颜色

c# - 自定义数字格式字符串以始终显示符号

c++ - 使用 cout << 运算符时,如何用前导零填充 int?

formatting - 如何更改 Visual Studio Code 中的缩进?

html - 从 XML 中的 html 格式的字符串资源设置 TextView 文本

c# - 如何使用 ToString() 格式化可为空的 DateTime?

c# - 如何在 .NET 中使用自定义格式对 TimeSpan 对象进行 String.Forma

ruby-on-rails - 在ruby中将 Camel 大小写转换为下划线大小写