Print in one line after removing previous in Python

I want to print only current index in my Python code.

for my_index in range(100):     print(my_index) 

This script currently prints like:

>>>python3 main.py 0 1 2 3 4 ... 

But I want to print only current my_index in one line without going to new line (or adding space between) like this:

>>>python3 main.py 0 

And then 0 cahnges to 1 like so…

>>>python3 main.py 1 

I DON’T want it like this:

>>>python3 main.py 0 1 2 3 4 ... 

And Python – Print in one line dynamically – Stack Overflow isn’t answer to my question.

Thanks in advance!

Add Comment
1 Answer(s)

Is this what you’re looking for?

You can use sys.stdout.write('\b \b') to delete a character already printed to the console. (Note: won’t work in IDLE)

A list of these "special characters" (known as string literals) can be found here

Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.