Asked  7 Months ago    Answers:  5   Viewed   190 times

If I had the following code:

for x in range(10):
     print x

I would get the output of

1
2
etc..

What I would like to do is instead of printing a newline, I want to replace the previous value and overwrite it with the new value on the same line.

 Answers

53

Simple Version

One way is to use the carriage return ('r') character to return to the start of the line without advancing to the next line.

Python 3

for x in range(10):
    print(x, end='r')
print()

Python 2.7 forward compatible

from __future__ import print_function
for x in range(10):
    print(x, end='r')
print()

Python 2.7

for x in range(10):
    print '{}r'.format(x),
print

Python 2.0-2.6

for x in range(10):
    print '{0}r'.format(x),
print

In the latter two (Python 2-only) cases, the comma at the end of the print statement tells it not to go to the next line. The last print statement advances to the next line so your prompt won't overwrite your final output.

Line Cleaning

If you can’t guarantee that the new line of text is not shorter than the existing line, then you just need to add a “clear to end of line” escape sequence, 'x1b[1K' ('x1b' = ESC):

for x in range(75):
    print(‘*’ * (75 - x), x, end='x1b[1Kr')
print()
Tuesday, June 1, 2021
 
samrap
answered 7 Months ago
27

python3 is not Python syntax, it is the Python binary itself, the thing you run to get to the interactive interpreter.

You are confusing the command line with the Python prompt. Open a console (Windows) or terminal (Linux, Mac), the same place you'd use dir or ls to explore your filesystem from the command line.

If you are typing at a >>> or In [number]: prompt you are in the wrong place, that's the Python interpreter itself and it only takes Python syntax. If you started the Python prompt from a command line, exit at this point and go back to the command line. If you started the interpreter from IDLE or in an IDE, then you need to open a terminal or console as a separate program.

Other programs that people often confuse for Python syntax; each of these is actually a program to run in your command prompt:

  • python, python2.7, python3.5, etc.
  • pip or pip3
  • virtualenv
  • ipython
  • easy_install
  • django-admin
  • conda
  • flask
  • scrapy
  • setup.py -- this is a script you need to run with python setup.py [...].
  • Any of the above together with sudo.

with many more variations possible depending on what tools and libraries you have installed and what you are trying to do.

If given arguments, you'll get a SyntaxError exception instead, but the underlying cause is the same:

>>> pip install foobar
  File "<stdin>", line 1
    pip install foobar
              ^
SyntaxError: invalid syntax
Tuesday, June 1, 2021
 
QuantumMechanic
answered 7 Months ago
64

You might be tried to change Settings -> Colors & Fonts -> Scheme name like below:

enter image description here

Try to edit Settings -> Appearances -> Theme:

enter image description here

Sunday, August 8, 2021
 
Sisyphus
answered 4 Months ago
58

use the following to convert to a timestamp in python 2

int((mod_time.mktime(first_run.timetuple())+first_run.microsecond/1000000.0))

Sunday, August 22, 2021
 
waylaidwanderer
answered 4 Months ago
13

Check this curses library, The curses library supplies a terminal-independent screen-painting and keyboard-handling facility for text-based terminals. An example:

x.py:

from curses import wrapper
def main(stdscr):
    stdscr.addstr(1, 0, 'Program is running..')  
    # Clear screen
    stdscr.clear()  # clear above line. 
    stdscr.addstr(1, 0, 'hello')
    stdscr.addstr(2, 0, 'dude')
    stdscr.addstr(3, 0, 'Press Key to exit: ')
    stdscr.refresh()
    stdscr.getkey()

wrapper(main)
print('bye')

run it python x.py

Wednesday, October 13, 2021
 
PJQuakJag
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :  
Share