2

This question is not duplicated, the linked and question not solve the problem, i'm trying print two lines at same position (each print in one row without new lines inside the loop), the linked question show how print just one line in the same position....

can be easy print one line in the same position inside a loop using python:

from time import sleep

for i in range(50):

  sleep(0.1)
  print("hi"+str(i) , end="\r")

But how I can do it using two lines inside the loop?

I tried:

from time import sleep

for i in range(50):

  sleep(0.1)
  print("hi"+str(i) , end="\r")
  print("hi2"+str(i) , end="\r")

It does not work :-(

I try (without the first \r):

  print("hi"+str(i) , end="")
  print("hi2"+str(i) , end="\r")

Not works too, this print hi and hi2 in the same line...

There are some way to print hi and hi2(each in a row) keeping the position line?

the expected result would be::

hi<increment from 0 to 50 without new line jump inside the loop>
hi2<increment from 0 to 50 without new line jump inside the loop>
  • I already change it, you have to be precise in your question. read [ask] – eyllanesc May 22 '18 at 01:49
  • 1
    If I understand you correctly, you always want to print two lines on each iteration, and always overwrite the two previous lines. In other words "print two lines in-place" but with two different print statements. As far as I know, this is not possible but perhaps someone else can help. – Bram Vanroy May 22 '18 at 01:53
  • 1
    You can use a library like `curses` to do more complicated printing. – BallpointBen May 22 '18 at 01:55
  • @BramVanroy yes, that's what I would like to do – user2721828 May 22 '18 at 01:58

1 Answers1

0
for i in range(50):
    sleep(0.1)
    print("hi"+str(i))
    print('hi2'+str(i), end='\r')

    if i<49:   
        print('\033[2F') # \033[2F,escape character to move cursor to the two lines up

hope it can help you

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Kei H
  • 55
  • 7
  • @user2721828 for windows, you can install a python package named colorama, and use escape character \033[2A to move cursor up – Kei H May 23 '18 at 02:27