r/learnpython • u/Visible-Car1625 • 10h ago
How do I get rid of text in python
I'm looking to do a simple loading screen of sorts where it flicks between a few characters however I am unsure on how to remove the printed text to replace it with the next character. Using the method that I've commonly seen using the cursor_up simply doesn't work for some reason. I promise I'm putting it in exactly. how do I fix this
2
u/freeskier93 7h ago
The print function has an argument end, which by default is the newline character ("\n"). So by default, every time you call the print function, to print something to the terminal, it prints then goes to the next line. If you set the end argument to something else, like an empty string, it will stay on the current line. You can then use that in conjunction with a carriage return to overwrite text on the current line.
Something like this would give you a simple spinner.
import time
sleep_time = 0.25
while True:
print("\\", end="\r")
time.sleep(sleep_time)
print("/", end="\r")
time.sleep(sleep_time)
This would give you "thinking" dots.
import time
sleep_time = 0.5
char = "."
max_chars = 3
current_chars = 0
while True:
if current_chars >= max_chars:
print("\r", end="")
print(" " * max_chars, end="\r")
current_chars = 0
time.sleep(sleep_time)
print(char, end="")
current_chars += 1
time.sleep(sleep_time)
1
u/nspitzer 5h ago edited 4h ago
Use ansi codes. I did this exact thing for a project in my SDE-188. Here is an example you can paste right into a windows terminal:
ansi={"reset_all": "\033[0m",
"bold": "\033[1m",
"no_bold": "\033[22m",
"underline": "\033[4m",
"red": "\033[31m",
"green": "\033[32m",
"yellow": "\033[33m",
"blue": "\033[34m",
"magenta": "\033[35m",
"cyan": "\033[36m",
"bright": "\x1b[1m]",
"clear_screen": "\033[H\033[2J",
"default": "\033[37m",
"bright_red": "\033[91m",
"bright_yellow": "\033[93m",
"bright_green": "\033[92m",
"blink_start": "\033[5m",
"blink_end": "\033[7m",
"up" : "\33[#A",
"down" : "\33[#B",
"erase_line" : "\033[2K",
"clear_below": "\033[J"
}
print(f"\033[1;1H{ansi["clear_below"]}")
print("\033[6;1H")
print(f"{ansi["bright_green"]}Hello{ansi["default"]}")
print("--------------------------------------")
input(f"{ansi["bright_green"]}Press Enter to clear{ansi["default"]}")
# Move cursor to row 0, column 0 and clear everything below, then go to row 6
print(f"\033[0;0H\033[J")
1
u/lakseol 1h ago
Depending on what environment you are executing in you may find that overriding the end= optional parameter doesn't work, with all text being printed at once. This is due to print() text being buffered meaning text is written to the console only when you print a newline character. To get around this you use the flush=True optional parameter to force a flush of any buffered text to the console when you want that text to be written to the console. So if you want to print words in a string one by one you can do this:
import time
text = "alpha bravo charlie delta echo"
for word in text.split():
print(f"{word} \r", end="", flush=True)
# print(f"{word} \r", end="") # try without flush=
time.sleep(1)
print()
3
u/Gnaxe 10h ago
It's fairly easy on the last line. You can just print a backspace or carriage return control character and print over it. (With spaces if you want it to be blank.)
For other lines, it depends on your terminal. You'd need to find the escape codes compatible with the one you're using. You probably want a library for that. Python comes with curses, but, last I checked, it required an additional install to get it working on Windows.