首页 > 解决方案 > Noob Question How would I update a print a function's output without creating a new line in my term in python3?

问题描述

I'm not exactly sure how to word this to be honest, but here's a simplified version of my current code.

for idx in range(0,11):
    print('Failed:',idx)

This outputs:

Failed: 0
Failed: 1
Failed: 2
Failed: 3
Failed: 4
Failed: 5
Failed: 6
Failed: 7
Failed: 8
Failed: 9
Failed: 10

I'm trying to replace the original int with the next int in the range instead of spamming my terminal with a new line for every int in the range.

How could I update "Failed: 1" to "Failed: 2" without creating a new line for "Failed: 2"?

Sorry if this is poorly worded, if you're willing to help me but like me to elaborate further please say so and I will try my best.

标签: python-3.x

解决方案


函数 print 需要第二个参数来指定 print 在行尾应该做什么

print("Text here", end="")

推荐阅读