【问题标题】:using argument end=" " for print function in Python 3.3在 Python 3.3 中使用参数 end="" 打印函数
【发布时间】:2013-09-17 19:24:28
【问题描述】:

考虑以下代码:

i=0
while i<5:
    print(i, end=" ")
    i = i + 1

导致输出:

0 1 2 3 4

如果我想在 1 2 3 4 之前添加一个字符串“结果”,则预期输出:结果 1 2 3 4(在同一行)。我应该使用什么内置函数?

【问题讨论】:

  • 你的意思是 - result 0 1 2 3 4
  • 是的。正是我的意思
  • 你应该在循环之前使用print
  • @larsmans 您能否将其发布为答案?谢谢。

标签: python python-3.x


【解决方案1】:
i = 0
print('the result', end=' ') # begin line with 'the result'
while i < 5:
    print(i, end=' ')
    i = i + 1
print()  # move to next line

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多