TheMeaningfulEngineer
TheMeaningfulEngineer

Reputation: 16359

What is the difference between the meaning of 0x and \x in Python hex strings?

I'm doing some binary operations which are often shown as hex-es. I have seen both the 0x and \x as prefixes.

In which case is which used?

Upvotes: 33

Views: 64654

Answers (2)

Euler
Euler

Reputation: 355

0x follows number, means HEX number

\x follows number, means HEX ascii characters

check it here: ascii table

Upvotes: -3

John La Rooy
John La Rooy

Reputation: 304375

0x is used for literal numbers. "\x" is used inside strings to represent a character

>>> 0x41
65
>>> "\x41"
'A'

>>> "\x01" # a non printable character
'\x01'

Upvotes: 51

Related Questions