Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are \t and \r in byte representation?

import sys
for i in range(30):
#     a = int(str(i),base = 16).to_bytes(4,sys.byteorder)
    a = i.to_bytes(4,sys.byteorder)
    print(a)

Here sys.byteorder seems to be 'little'. The output of the above code is:

b'\x00\x00\x00\x00'
b'\x01\x00\x00\x00'
b'\x02\x00\x00\x00'
b'\x03\x00\x00\x00'
b'\x04\x00\x00\x00'
b'\x05\x00\x00\x00'
b'\x06\x00\x00\x00'
b'\x07\x00\x00\x00'
b'\x08\x00\x00\x00'
b'\t\x00\x00\x00'
b'\n\x00\x00\x00'
b'\x0b\x00\x00\x00'
b'\x0c\x00\x00\x00'
b'\r\x00\x00\x00'
b'\x0e\x00\x00\x00'
b'\x0f\x00\x00\x00'
b'\x10\x00\x00\x00'
b'\x11\x00\x00\x00'
b'\x12\x00\x00\x00'
b'\x13\x00\x00\x00'
b'\x14\x00\x00\x00'
b'\x15\x00\x00\x00'
b'\x16\x00\x00\x00'
b'\x17\x00\x00\x00'
b'\x18\x00\x00\x00'
b'\x19\x00\x00\x00'
b'\x1a\x00\x00\x00'
b'\x1b\x00\x00\x00'
b'\x1c\x00\x00\x00'
b'\x1d\x00\x00\x00'

Observe integer 9 here is written obnoxiously as b'\t\x00\x00\x00' along with similar oddities such as 0xa and 0xd.

Is this an aberration or am I lacking knowledge of these notation? My Python version is 3.8.2.

like image 535
PrisionMike Avatar asked Sep 09 '25 11:09

PrisionMike


1 Answers

These are escape sequences.
\t represents an ASCII Horizontal Tab (TAB) and \r represents an ASCII Carriage Return (CR).

See Python's documentation of String and Bytes literals.

like image 185
Harmon758 Avatar answered Sep 11 '25 01:09

Harmon758