1

I am reading a data frame sent by a hardware device that separates each field of the frame with the character '\r'. When printing the frame in python 2, I have found differences between print and print() and some problems with disappearing characters.

Some differences between print() and print can be found in: What is the difference between print and print() in python 2.7 but that not explains the problems that I'm having.

For example, when executing

>>>frame = 'word_1#\rword_2\r'
>>>print(frame)
word_2#
>>>print frame
word_2#

They are both the same, but I don't understand why the '#' is moved to the end of 'word_2' and why 'word_1' disappears.

Also, print and print() show different results in:

>>>frame = 'word_1#\rword_2\r'
>>>print('data:', frame)
('data', 'word_1#\rword_2\r')  
>>>print 'data:', frame
word_2word_1#

Here print() seems to work as expected, but print has removed 'data' word and has changed the order of the words.

Finally, it is also confusing this case:

>>> frame = 'word_1\r#word2\r#'
>>> print(frame)
#word2
>>> print frame
#word2
>>> print('data', frame)
('data', 'word_1\r#word2\r#')
>>> print 'data', frame
#word2ord_1

where print(), print and print('data', frame) seems to work the same way that they did in the previous cases but print 'data', frame has removed the initial 'w' from word_1 after changing the order.

What's happening here?

3 Answers3

1

The '\r' character is called carriage return. Its goal is to put the cursor on the beginning of the line.

I will re-wrote you code to explain better what it does : (| is your cursor)


>>> frame = 'word_1\r#word2\r#'
>>> print(frame)
word_1|                            # write word_1
|word_1                            # put cursor on the beginning of the line
#word2|                            # write #word2 but on top of word_1 since your cursor was at the beginning of the line
|#word2                            # put cursor on the beginning of the line
#|word2                            # rewrite the # on top of the previous one

>>> print frame                    # Same things happens here
#word2
>>> print('data', frame)
('data', 'word_1\r#word2\r#')      # As pointer by U10-Forward this is printing an actual tuple
>>> print 'data', frame
data|                              # you write data
dataword_1|                        # you write word_1
|dataword_1                        # you put the cursor on the beginning of the line
#word2ord_1|                       # you write #word2 on top of the previous print
|#word2ord_1                       # cursor on the beginning of the line
#|word2ord_1                       # you write again the #

BTW: Use Python3 unless you are required to use Python2

Tiphaine
  • 193
  • 10
1

They are both the same, but I don't understand why the '#' is moved to the end of 'word_2' and why 'word_1' disappears.

\r is carriage return which means it will move the print pointer to the starting of the line and then start overwriting the output following the \r.

So in your case as the no of characters in word_2 is same as that of word_1 so after the first \r it overwrites word_1 to produce the output as word_2#.

Ankit Agrawal
  • 616
  • 9
  • 20
0

Because in Python 2:

print(...)

Is processing a regular print ... but just simply printing a tuple, while the other isn't.

U13-Forward
  • 69,221
  • 14
  • 89
  • 114