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?

4
  • 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. Commented May 6, 2020 at 13:50
  • 1
    Does this answer your question? What's the correct way to convert bytes to a hex string in Python 3? Commented May 6, 2020 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. Commented May 6, 2020 at 13:53
  • There's also this: stackoverflow.com/questions/13435922/python-encode Commented May 6, 2020 at 13:53

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.