Print() to previous line?

There are times when you cannot control the print statement that proceeds yours (or doing so may be difficult). This then will:

  • go to the (start of the) previous line: \033[F
  • move along ncols: \03[{ncols}G
  • start printing there.
print(f"\033[F\033[{ncols}G Space-lead appended text")

I have not found a way of going to the "end" of the previous line, but you can specify a value of ncols which is greater that the length of the previous line. If it's shorter that the previous line, you will end up overwriting what was there.


In Python 3, you can suppress the automatic newline by supplying end="" to print():

print("Random string value", end="")
if a==0:
    print(" is random")
else:
    print()

See How to print without newline or space?