0

If X = "ABCD" then encoding the X should show: \x41\x42\x43\x44

But why does X.encoding("utf-8") shows: b"ABCD"

I am having difficulties in understanding how text encoding works in python?

Torxed
  • 22,866
  • 14
  • 82
  • 131
jamilonly
  • 39
  • 6
  • 2
    The two things you showed, are the same thing. The terminal will interpret `\x41` and print `A` for you. `b'\x41' == b'A'` will be `True`. And that's because, again, they're the same - you're just seeing a representation of `\x41`. – Torxed May 06 '20 at 13:50
  • 1
    Does this answer your question? [What's the correct way to convert bytes to a hex string in Python 3?](https://stackoverflow.com/questions/6624453/whats-the-correct-way-to-convert-bytes-to-a-hex-string-in-python-3) – Torxed May 06 '20 at 13:52
  • `bytes` are shown as user-friendly as possible. But both `\x41\x42\x43\x44` and `ABCD` are just two different representations of the same information. – Klaus D. May 06 '20 at 13:53
  • There's also this: https://stackoverflow.com/questions/13435922/python-encode – Torxed May 06 '20 at 13:53

1 Answers1

1

Those are the same thing - bytes are just shown in human-readable form whenever it's possible. :)

Try it yourself in Python console:

>>> b"\x41\x42\x43\x44"
b'ABCD'
>>> "ABCD".encode() == b"\x41\x42\x43\x44"
True
h4z3
  • 5,265
  • 1
  • 15
  • 29