Cara menggunakan binary string type python

Cara menggunakan binary string type python

Table of Contents

  • Creating bytes objects in Python
  • Strings represent text, bytes objects represent binary data
  • Where are bytes objects used in Python?
  • How to convert bytes into a string
  • How do you write binary data in Python?
  • Is binary a data type in Python?
  • How do you decode a binary text in Python?
  • How do you convert a binary program to a decimal in Python?

3 minute read Python 3.7—3.10

Watch as video

03:25

Sign in to your Python Morsels account to save your screencast settings.

Don't have an account yet? Sign up here.

Let's talk about the difference between strings and bytes in Python.

Creating bytes objects in Python

Strings represent text (human language that is). For example, here we have a string named text:

But there's another type that's closely associated with strings, which kind of looks like making a string with a b prefixed in front of it.

That b is sort of like an f before an f-string, or an r before a raw string. But that b doesn't actually make a string, it makes a bytes object:

>>> data
b'hello'
>>> type(data)
<class 'bytes'>

Strings represent text, bytes objects represent binary data

If we loop over a string in Python, we'll get back sub-strings representing each of the characters in that string:

>>> text = "hello"
>>> list(text)
['h', 'e', 'l', 'l', 'o']

What do you think we'll get if we loop over a bytes object?

>>> data = b"hello"
>>> list(data)

Since bytes objects represent binary data, when we loop over them we get back numbers (from 0 to 255) representing each of the bytes in that binary data:

>>> data = b"hello"
>>> list(data)
[104, 101, 108, 108, 111]

We can also do the opposite of this. We can take an iterable of numbers and turn it into a bytes object by passing it to the bytes constructor:

>>> nums = [0, 65, 97, 255]
>>> bytes(nums)
b'\x00Aa\xff'

Where are bytes objects used in Python?

All data that comes from outside of our Python process starts as bytes. But if that data represents text (and Python knows it) Python will convert it to strings automatically.

If we use the urllib module in Python to do an HTTP request, the data that we get back is not represented as a string:

>>> from urllib.request import urlopen
>>> data = urlopen('https://pseudorandom.name').read()
>>> data
b'Grace Jones\n'
>>> type(data)
<class 'bytes'>

The data we get back is represented as a bytes object because it might not even represent text. After all, an HTTP request can send back any data, even arbitrary binary data.

If we open up a file with the mode of rb, we're opening that file not in the default read-text mode, but instead in read-binary mode.

>>> with open("avatar.jpg", mode="rb") as jpg_file:
...     jpg_data = jpg_file.read()
...

So when we read from that file, the data that we get out of it will not be a string, it'll be a bytes object.

>>> type(jpg_data)
<class 'bytes'>

In fact in this case where we're opening up a jpg file, we get a bytes object with a lot of bytes in it, because it takes a lot of bytes to represent an image:

>>> len(jpg_data)
1108051

How to convert bytes into a string

If you end up with a bytes object in Python, and you know that that object represents text, you can turn it into a string by calling its decode method:

>>> data = b"bytes! \xe2\x9c\xa8"
>>> data.decode()
'bytes! ✨'

The decode method (without any arguments passed to it) uses a default character encoding of utf-8. Even if we know that the data we're working with uses that default character encoding of utf-8, it's considered a best practice to always specify the encoding of our bytes:

>>> text = data.decode("utf-8")
>>> text
'bytes! ✨'

As the Zen of Python says, "explicit is always better than implicit".

If for some reason you have a string you want to turn it into bytes, you can call the encode method on that string to encode it into bytes:

>>> text.encode()
b'bytes! \xe2\x9c\xa8'

Just like decode, the encode method defaults to using utf-8, but you could specify a different character encoding if you wanted to:

>>> text.encode("utf-8")
b'bytes! \xe2\x9c\xa8'
>>> text.encode("utf-16-le")
b"b\x00y\x00t\x00e\x00s\x00!\x00 \x00('"

Summary

Strings represent text-based data, while bytes represent binary data (i.e. images, video, or anything else you could represent on a computer).

Depending on what you use Python for, you probably won't encounter bytes objects very often. But when you do, the one thing you'll probably want to do with them is call their decode method to turn them into a string (assuming those bytes represent text).

A Python Tip Every Week

Need to fill-in gaps in your Python skills? I send weekly emails designed to do just that.

How do you write binary data in Python?

How to write to a binary file in Python.

file = open("sample.bin", "wb").

file. write(b"This binary string will be written to sample.bin").

file. close().

Is binary a data type in Python?

Python has the following data types built-in by default. ... 1. Built-in Data Types in Python..

How do you decode a binary text in Python?

Method #1: The binary data is divided into sets of 7 bits because this set of binary as input, returns the corresponding decimal value which is ASCII code of the character of a string. This ASCII code is then converted to string using chr() function.

How do you convert a binary program to a decimal in Python?

Python Math: Convert a binary number to decimal number.

Sample Solution:-.

Python Code: b_num = list(input("Input a binary number: ")) value = 0 for i in range(len(b_num)): digit = b_num.pop() if digit == '1': value = value + pow(2, i) print("The decimal value of the number is", value) ... .

Pictorial Presentation:.

Flowchart:.