0

I am trying to write a program that will emulated a 20x4 character LCD screen by printing data dynamically to a terminal. Currently I am just trying to get the output to the terminal to work but I can't figure out how to print on multiple lines concurrently without using new line characters.

import time

i = 0
for i in range(0, 9):
    print(str(i) + str(i) + str(i) + str(i) + str(i) +
          str(i) + str(i) + str(i) + str(i) + str(i) +
          str(i) + str(i) + str(i) + str(i) + str(i) +
          str(i) + str(i) + str(i) + str(i) + str(i), end='\r')
    print(str(i) + str(i) + str(i) + str(i) + str(i) +
          str(i) + str(i) + str(i) + str(i) + str(i) +
          str(i) + str(i) + str(i) + str(i) + str(i) +
          str(i) + str(i) + str(i) + str(i) + str(i), end='\r')
    print(str(i) + str(i) + str(i) + str(i) + str(i) +
          str(i) + str(i) + str(i) + str(i) + str(i) +
          str(i) + str(i) + str(i) + str(i) + str(i) +
          str(i) + str(i) + str(i) + str(i) + str(i), end='\r')

    time.sleep(1)

Currently this code prints one line of 20 characters that updates correctly but I need the additional two lines below it. The expected output I would like is 4 lines of 20 characters each that updates dynamically. Eventually, each line would just be one string for the 20 characters.

2 Answers2

0

You should be able to achieve this using f string format and docstring.

Refer to the Official Documentation for F Strings(formatted string literals) here

The code should look like this.

import time

i = 0
for i in range(0, 9):
    print(f"""
          {str(i)} {str(i)} {str(i)} {str(i)} {str(i)} 
          {str(i)} {str(i)} {str(i)} {str(i)} {str(i)}
          {str(i)} {str(i)} {str(i)} {str(i)} {str(i)} 
          {str(i)} {str(i)} {str(i)} {str(i)} {str(i)} """)

    time.sleep(1)

Please let me know if this help if it's not what you are looking for let me know so I can help you figure something more suitable for your needs.

Rodrez
  • 1
  • 4
  • wouldnt that just print 4 lines, then 4 lines under it, then 4 more lines? not what OP is asking. – JL Peyret Mar 18 '20 at 17:56
  • My apologies I misunderstood I'll provide another piece of code – Rodrez Mar 18 '20 at 18:01
  • no worries. welcome aboard though. dont think too much about different code, i really doubt it's very easy to do in pure Python without specialized libraries to print in place (something like what `top` does in Unix). unless there's a reverse linefeed, something that goes back up. – JL Peyret Mar 18 '20 at 18:06
  • hah! https://stackoverflow.com/questions/11474391/is-there-go-up-line-character-opposite-of-n have fun! – JL Peyret Mar 18 '20 at 18:07
  • Lol interesting – Rodrez Mar 18 '20 at 18:22
  • I read the referenced other question but not sure how that applies here, can you elaborate? – iSynthetixx Mar 18 '20 at 19:58
0

Adjusting Rodrez's answer slightly:

import time

i = 0

goback = "\033[F" * 5  #  this "climbs you back up 5 lines" at the next
                       # iteration, output at same spot, like `top`


for i in range(0, 9):
    print(f"""{goback}
          {i} {i} {i} {i} {i} 
          {i} {i} {i} {i} {i}
          {i} {i} {i} {i} {i} 
          {i} {i} {i} {i} {i} """)

    time.sleep(1)


see https://stackoverflow.com/a/11474509 for where I got the weird characters from.

JL Peyret
  • 10,917
  • 2
  • 54
  • 73
  • unfortunately, this prints out multiple new lines. – iSynthetixx Mar 21 '20 at 23:54
  • doesn't for me, on a mac. linux would be same, most likely. windows? no idea - 33f is a character code for nix shell terminals I believe. make sure you have goback at the correct spot and with the correct content. you may also have to tweak `5` to be whatever the amount of lines your templates uses each time. i.e. if 5 lines, then 5 33f back up. – JL Peyret Mar 22 '20 at 03:37
  • I believe it needs to be in an ANSI terminal. Works well here on gnome-terminal on Linux. – scottkosty Jan 18 '23 at 01:34