Search code examples
pythonprintingterminalcarriage-return

Missing characters when printing a string in terminal


I'm printing a string to the terminal (iTerm on OSX).

My code looks like this:

print("1234567890")
print(summary)
print("XX" + summary)
print(ord(summary[0]), summary[0])
print(ord(summary[1]), summary[1])

The output looks like this:

terminal output

Note how the first two characters from any print statement containing that summary string are deleted. I've printed out the first two characters and their ASCII codes, and they are what I'd expect (E and x).

Printing strings has never been a problem before.

I can't provide an example of the summary string, but if I run

print(repr(summary))

it shows that there is \r \r at the end. Could that affect the first two characters? What could I possibly look at to start debugging this?


Solution

  • print(repr(summary)
    

    shows:

    'Example from ma....\r \r '
    

    On iTerm on OSX, the first \r returns to the start of the line, the subsequent space overwrites the E. I don't entirely understand how the x gets erased, since I would have thought the next \r returns to the start of the line again.

    Nevertheless, this was enough to fix the problem.