22

I need command that writes to previous line, like print() without \n.

Here is some example code:

a=0

print("Random string value")

if a==0:
    print_to_previous_line("is random")

and output

Random string value is random

i know that i can do like print("string", value) to use multipile different things in same print command, but that is not the answer. Reason is too messy to be explained here, but this "print to previous line" will be exactly right answer. So what would it be in reality?

Jabutosama
  • 403
  • 2
  • 4
  • 10
  • Question is about print, but what about input where something is written to screen as part of it? If you do a second input it will right to the next line, but perhaps you would rather overwrite instead of print line and line after line – demongolem May 22 '20 at 12:25

5 Answers5

30

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?

Community
  • 1
  • 1
NPE
  • 486,780
  • 108
  • 951
  • 1,012
27

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.

Nuno André
  • 4,739
  • 1
  • 33
  • 46
Enda Farrell
  • 778
  • 9
  • 15
3

I would suggest using the print statement, like this

print("This is a text",end=" ")

The end=" " says, that the string isn´t 'complete' and the next print statement needs to come in the same Line. The String " " means, that it should leave a space between this String and the String in the next print statement. Alternatively, you could use end="" too. I hope this Helped!

Alex44
  • 3,597
  • 7
  • 39
  • 56
2

I would suggest printing a statement like this first:

print("Some stuff here", end=" ")

Then I would use an if statement like this:

if stuff:
     print("Text you want displayed on same line")
else:
     print("")
     print("Text you want displayed on next line")
Meh
  • 21
  • 2
  • Could you explain with more details? – E.Coms Oct 15 '18 at 21:37
  • Yes, the first statement sets it up so that you can print to the same line. Then if "stuff" is true, it will print to the same line. If stuff is not true then the first print statement will print nothing on the line and automatically start the next line. Then the second print statement will print what is going to be displayed on the next line. – Meh Oct 16 '18 at 21:57
  • Make sense now. – E.Coms Oct 16 '18 at 22:40
  • I'm glad I could help. @E. Coms – Meh Oct 16 '18 at 23:03
1
def Fib(n):
    a,b = 0,1
    c=','
    while(a<n):
       print(a, end='')   # Way to print in same line
       a,b = b, a+b
       if a<n:
         print(c, end=',') # Way to ensure keep printing in same line
Fib(1000)