3

I am trying to remove extra characters printed out in my print statement from another print statement.

Here is what my code looks like:

print(addedlist)  #addedlist = [9,5,1989,1,2,3,4,5,6,7,8,9]

for x in range(0, len(addedlist)):
    print('%d->'%addedlist[x],end="")

print('\n')

the output of this looks like this:

[9, 5, 1989, 1, 2, 3, 4, 5, 6, 7, 8, 9]
9->5->1989->1->2->3->4->5->6->7->8->9->  

I am trying to remove the last -> characters. I tried doing :

print(addedlist)  #addedlist = [9,5,1989,1,2,3,4,5,6,7,8,9]

for x in range(0, len(addedlist)):
     print('%d->'%addedlist[x],end="")

print('\b\b\n')

but it didn't work.

How would i go about accomplishing this ?

EDIT:

Just some clarification, I know i can change my original print statement to print it more correctly to avoid trailing -> ... I am after a solution for how to erase trailing '->' this once the mistake has been made

1
  • You'll need something more powerful than just print commands. Commented Apr 18, 2014 at 7:28

2 Answers 2

6

I notice you wants to play with carriage return commands. First you need to learn both \b (moves the active position to the previous position) and \r (moves the active position to the start of line) works for current active line.
I think you are working on command line interpreter; at which you explicitly press enter for next line.
On command line interpreter use ; as follows:

>>> print("abb", end=""); print("\b\bcc")
acc
>>> print("a->", end=""); print("\b\b  ")
>>> a

If you are using some script then your code should work, see:

Upload$ cat s.py
print ("abcd->", end="")
print ("\b\b  ")
Upload$ python3.2 s.py
abcd    # notice -> is removed  

But this is not much useful, correct approach is what Aशwini चhaudhary has shown in his answer.

Edit: I found you mistake in your code

print('%d->' % addedlist[x], end="")    
print('\b\b\n')

You use '\b' to moves the active position to the previous position, but you don't overwrite "->", you just outputs '\n' that shift cursors to next line, you should rectify your code as below.

print('%d->' % addedlist[x], end="")    
print('\b\b   \n')
#          ^^^  /b then overwrite with spaces 

Run at Linux shell as $ python scriptname.py (For command line Python's interpreter you can use something like code I written, it is just to play, use str.join or use sep parameter in print()).

Sign up to request clarification or add additional context in comments.

4 Comments

thanks mate. Looking at your command line interpreter code, how would you impliment that in my for loop code ?
Thanks a lot.. that helps in command prompt... Why doesn't this work in python shell though ??
@sukhvir No it is working on python shell with ; as I shown. Note: on python interpreter you explicitly enters to run a comment that also give newline at output.
@sukhvir After playing around 5 min with your code I could write a code for command line interceptor Copy-past this code at Python's command line interpreter that will run like this image
4

Use str.join:

>>> s = [9,5,1989,1,2,3,4,5,6,7,8,9]
>>> print ('->'.join(map(str, s)))
9->5->1989->1->2->3->4->5->6->7->8->9

Or better print() with sep as '->':

>>> print(*s, sep='->')
9->5->1989->1->2->3->4->5->6->7->8->9

2 Comments

thanks .. I am aware of how to change my print statement to print it correctly .. I am interested in correcting it after wrong printing has been done
@sukhvir No once you have printed on stdout you can't overwrite it (if you are not working in curses library). So the answer posted here is exactly do what you wants, otherwise learn ncurses mode.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.