##
##
This kind of pattern
#####
######
I'd like to output it side by side like this repeatedly.
for_inrange(3):
print("##\n##")
does not print to the first line again.
What should I do?
Also, I would appreciate it if you could tell me how to store these outputs in variables.
Output the ANSI CUU (Cursor UP).
import sys
import time
for i in range (3):
bar="##"*(i+1)
sys.stdout.write(f"{bar}\n")
sys.stdout.write(f"{bar}\r")
sys.stdout.write("\033[1A\r")
time.sleep(1)
sys.stdout.write("\n\n")
It's the same content as this article we recently exchanged.
How to reduce flickering when redrawing in Powershell
The ANSI VT100 escape sequence allows you to move the cursor and color the screen.
Python will be able to use curses.
However, it says that you can use UniCurses instead because it is not for Windows.
Python for Windows does not include curses. A ported version named UniCurses is available.You may also want to try the Console module by Fredrik Lundh.This does not use the same API as curses, but provides full support for cursor positioning text output and mouse and keyboard input.
add
After that, I found that UniCurses only supports 32-bit environments.
There was an alternative windows-curses, but it was not related to this question, but it was difficult to use full-width characters, so I put it on hold.
I found the mode setting process in the original English introduction, so I put it in.
import platform
import time
# Set mode at Windows 10 1607 or later command prompt (not previously supported?)
# PowerShell doesn't need it, but you can do it does.
if platform.system() == 'Windows':
from ctypes import windll, wintypes, byref
STD_OUTPUT_HANDLE=-11
ENABLE_VIRTUAL_TERMINAL_PROCESSING=0x0004
hOut=windll.kernel32.GetStdHandle(wintypes.HANDLE(STD_OUTPUT_HANDLE))
dwMode=wintypes.DWORD()
result = windll.kernel32.GetConsoleMode(hOut, byref(dwMode))
dwMode.value | = ENABLE_VIRTUAL_TERMINAL_PROCESSING
result=windll.kernel32.SetConsoleMode(hOut,dwMode)
loopcount = 3 # number of loops
lines = 2# Number of lines displayed
cursorback='\r'+'\x1B['+str(lines-1)+'A'# string to return the cursor
for i in range (loopcount):
line='##'*(i+1)# From here
outstr=line
for_in range (lines-1):
outstr+='\n'+line# So far, the process of creating an output string and putting it into a variable.
print(outstr, end=')# string display
time.sleep(1)# Wait 1 second for display confirmation
if i>=(loopcount-1): # Ends without cursor back
print('\n')
break
print(cursorback, end=')# The action of returning the cursor for the next loop
Note:
How to overwrite and output the console with Python
Get OS and version information for environments running Python
curses-related
Fails to import pdcurses on windows 64bit#2
nIt's been 10 years since I used curses
Use curses module in Python
Python, create a simple editor with curses
© 2025 OneMinuteCode. All rights reserved.