PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, September 18, 2022

[FIXED] How can I delete printed line in python?

 September 18, 2022     printing, python-3.x     No comments   

Issue

I would love to print a counting down and in each second delete a line:

import time
for x in range(3):
    print(x)
    time.sleep(1)
    #Delete previous line

How can I do? Thanks


Solution

It all depends what terminal are you using. You can simulate the deletion of the line with carriage-return characters \r and spaces ' ', for example:

import time

for x in range(3):
    print(x, end='', flush=True)
    time.sleep(1)
    print('\r    \r', end='', flush=True)

For more advanced solution, look at the curses module.



Answered By - Andrej Kesely
Answer Checked By - Katrina (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing