4

When I print a program such as this in Python:

x = b'francis'

The output is b'francis'. If bytes is in 0's and 1's why is it not printing it out?

6
  • 3
    What makes you think a bytes object would print in binary? Commented Apr 16, 2018 at 18:51
  • Everything is in 0s and 1s, but that doesn't mean it's useful to display everything as 0s and 1s. Commented Apr 16, 2018 at 18:52
  • 1
    "bytes" != "bits" Commented Apr 16, 2018 at 18:54
  • Are you asking for the design rationale behind it working this way? Commented Apr 16, 2018 at 18:56
  • 1
    If you'd like to see the bits which make up the string: for byte in b"francis": print(format(byte, "08b")) Commented Apr 16, 2018 at 18:58

4 Answers 4

6

You seem to be fundamentally confused, in a very common way. The data itself is a distinct concept from its representation, i.e. what you see when you attempt to print it out or otherwise display it. There may be multiple ways to represent the same data. This is just like how if I write 23 (in decimal) or 0x17 (hexadecimal) or 0o27 (octal) or 0b10111 (binary) or twenty-three (English), I am talking about the same number.

At some lower level below Python, everything is bytes, and each byte consists of bits; but it is not correct to say that the bytes "are in" 0s and 1s - just like how it is not correct to say that the number twenty-three "is in" decimal digits (or hexadecimal, octal or binary ones, or in English text characters).

The symbols 0 and 1 are just pictures that we draw on a screen to represent the state of those bits - if we choose to represent them individually. Sometimes, we choose larger groupings, and assign different symbols to various combinations of states. For example, we may interpret multiple bits as a single integer value in binary; or (using Unicode) we might further interpret that number as a "code point" (most of these are text characters; some are control characters, or portions of text characters).

A Python bytes object is a wrapper for a "raw" sequence of bytes. When you display it, Python uses a representation where each byte (grouping of 8 bits) corresponds to one or more symbols: bytes whose corresponding integer value is between thirty-two and one hundred twenty-six (inclusive) are (for historical reasons) represented using individual text characters (following the so-called ASCII encoding), while others are represented with a four-character "escape sequence" beginning with \x and followed by the hexadecimal representation of the number.

Sign up to request clarification or add additional context in comments.

Comments

3

From python docs:

bytes and bytearray objects are sequences of integers (between 0 and 255), representing the ASCII value of single bytes.

So they are sequence of integers which represents ASCII values.

For conversion you can use:

import sys
int.from_bytes(b'\x11', byteorder=sys.byteorder)  # => 17
bin(int.from_bytes(b'\x11', byteorder=sys.byteorder))  # => '0b10001'

Comments

1

The bytes object was intentionally designed to work like this: the repr uses the corresponding ASCII characters for bytes in the printable ASCII range, well-known backslash escapes for a few special ASCII control characters, and hex backslash escapes for everything else (and the str just is the repr).

The basic idea is that bytes can be used as an immutable array of integers from 0-255, but more often it's used as an immutable array of characters encoded in some ASCII-compatible charset.

In particular, one of the most common uses of bytes is for things like the headers in HTTP, SMTP, and other network protocols. These headers are generally entirely in pure ASCII, or at least pure ASCII keys with some values in pure ASCII and others in an ASCII-compatible charset—and you generally have to parse the ASCII headers to figure out what charset to use to decode the body. Being able to see those headers are ASCII characters is a lot more useful than just seeing them as a sequence of numbers.

Comments

0

Basically, everything on your computer is eventually represented by 0's and 1's. The purpose of b-notation isn't as you expected it to be.

I would like to refer you to a great answer that might help you understand what the b-notation is for and how to use it properly: What does the 'b' character do in front of a string literal?

Good luck.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.