5

I am writing a program in Python and want to replace the last character printed in the terminal with another character.

Pseudo code is:

print "Ofen",
print "\b", # NOT NECCESARILY \b, BUT the wanted print statement that will erase the last character printed
print "r"

I'm using Windows8 OS, Python 2.7, and the regular interpreter.

All of the options I saw so far didn't work for me. (such as: \010, '\033[#D' (# is 1), '\r').

These options were suggested in other Stack Overflow questions or other resources and don't seem to work for me.

EDIT: also using sys.stdout.write doesn't change the affect. It just doesn't erase the last printed character. Instead, when using sys.stdout.write, my output is:

Ofenr # with a square before 'r'

My questions:

  1. Why don't these options work?
  2. How do I achieve the desired output?
  3. Is this related to Windows OS or Python 2.7?
  4. When I find how to do it, is it possible to erase manually (using the wanted eraser), delete the '\n' that is printed in python's print statement?
Ofer Arial
  • 1,129
  • 1
  • 10
  • 25

3 Answers3

7

When using print in python a line feed (aka '\n') is added. You should use sys.stdout.write() instead.

import sys
sys.stdout.write("Ofen")
sys.stdout.write("\b")
sys.stdout.write("r")
sys.stdout.flush()

Output: Ofer

Carles Mitjans
  • 4,786
  • 3
  • 19
  • 38
  • I edited my question - this doesn't work as well.. Is `'\b'` really the right character to use when using sys.stdout? – Ofer Arial Feb 18 '17 at 09:28
  • It should work, what are you trying? [Here](https://docs.python.org/2.0/ref/strings.html) you have a list of string literals. – Carles Mitjans Feb 18 '17 at 09:31
  • Only when I used flush it worked as wanted. Why is that? And can you address my 4th question as well? Why wouldn't that work? Does it have to do with flush too? – Ofer Arial Feb 18 '17 at 12:19
  • @OferArial When printing in standard output, python keeps it in a buffer. Using `flush` will empty that buffer. Take a look [here](http://stackoverflow.com/questions/10019456/usage-of-sys-stdout-flush-method). Using `print` statement will always append `'\n'` – Carles Mitjans Feb 18 '17 at 12:29
  • 1
    Got what flush does. So why writing out `'\b'` won't delete the `'\n'` written by print? – Ofer Arial Feb 18 '17 at 12:57
  • Hope [this](http://stackoverflow.com/questions/275018/how-can-i-remove-chomp-a-newline-in-python) answers your question :) – Carles Mitjans Feb 18 '17 at 13:30
  • Can it also remove if the last printed char is new line? – alper Aug 20 '21 at 13:09
5

You can also import the print function from Python 3. The optional end argument can be any string that will be added. In your case it is just an empty string.

from __future__ import print_function # Only needed in Python 2.X

print("Ofen",end="")
print("\b",end="") # NOT NECCESARILY \b, BUT the wanted print statement that will erase the last character printed
print("r")

Output

Ofer
StackJack
  • 145
  • 1
  • 9
-4

I think string stripping would help you. Save the input and just print the string upto the length of string -1 .

Instance

x = "Ofen"
print (x[:-1] + "r")

would give you the result

Ofer

Hope this helps. :)

RiddSann
  • 3
  • 3
Rahul Khanna
  • 59
  • 1
  • 2
  • 8