Eğitim / Kodlama 17 Nisan 2024

Python Bytes to String – How to Convert a Str to Bytes and Back Again

Python Bytes to String – How to Convert a Str to Bytes and Back Again

You can use bytes in Python to represent data in binary form. In this article, you’ll learn how to convert bytes to a string, and vice versa.

Before we look at the conversions, let’s talk about how bytes work in Python. You can skip to the next section if you already understand this, or you’re just interested in the conversions.

How Bytes Work in Python

You can create byte literals by prefixing the b notation. This tells the Python interpreter that a set of characters should be treated as bytes. Here’s an example:

byte_data = b'Hello'

In the code above, we prefixed b right before the string value: b'Hello'. If you print the characters in the string, you’ll get a binary value for each. That is:

byte_data = b'Hello'
print(byte_data[0]) 

So instead of “H”, 72 was returned. If you go on to print the value of each index in the sequence, you should get their binary values:

byte_data = b'Hello'
print(byte_data[0]) 
print(byte_data[1]) 
print(byte_data[2]) 
print(byte_data[3]) 
print(byte_data[4]) 

Now let’s talk about how to convert a string to bytes, and how to convert bytes to a string.

You can use the encode() method to convert a string to bytes in Python. The method simply encodes a string using a specific encoding like UTF-8, ASCII, and so on.

Here’s an example:

string_data = "Hello"
print(string_data[0]) 

In the code above, we created a string called string_data with a value of “Hello”. We also printed the first character of the string, which is “H”.

Now let’s convert the string to bytes using the encode() method:

string_data = "Hello"
byte_data = string_data.encode('utf-8')
print(byte_data[0]) 

We converted the string_data variable to bytes using the encode() method which took “utf-8” as a parameter. We stored this conversion in the byte_data variable: byte_data = string_data.encode('utf-8').

Lastly, we printed the first character of the byte_data variable and got a binary value: print(byte_data[0]) # 72.

You can use the decode() method to convert bytes to a string in Python. It works just like the encode() variable: attach the variable to be converted using dot notation and specify the encoding type as the method’s parameter.

Here’s an example:

byte_data = b'Hello'
string_data = byte_data.decode('utf-8')
print(string_data[0]) 

In the code above, we created a bytes object called byte_data.

Using the decode() method, we converted it to a string and stored it in a string_data variable: string_data = byte_data.decode('utf-8').

When you print the characters of the string_data variable, you should get string characters instead of binary values: print(string_data[0]) # H

In this article, you learned how to use bytes in Python. You also learned two conversion methods:

Happy coding!

source

Spread the love <3

You may also like...

Nis
20
2024
0
Harvard’s startup whisperer, Peter Gladstone, reveals secrets to validating consumer demand at TechCrunch Early Stage

Harvard’s startup whisperer, Peter Gladstone, reveals secrets to validating consumer demand at TechCrunch Early Stage

Validating consumer demand is a crucial step for any startup, and TechCrunch Early Stage is offering a golden opportunity to...

Spread the love <3
May
22
2024
0
Metinden reklama! Google arama sonucunda reklam dönemi başladı

Metinden reklama! Google arama sonucunda reklam dönemi başladı

Google, arama sonuçlarını daha da zenginleştirmek ve rakibi Microsoft’a karşı avantaj sağlamak amacıyla yapay zeka (AI) destekli reklam göstermeye başlıyor....

Spread the love <3
May
26
2024
0
Women in AI: Sarah Myers West says we should ask, ‘Why build AI at all?’

Women in AI: Sarah Myers West says we should ask, ‘Why build AI at all?’

To give AI-focused women academics and others their well-deserved — and overdue — time in the spotlight, TechCrunch has been...

Spread the love <3
May
03
2024
0
Intel, yapay zekayı masaüstüne getiriyor! İşte yeni Core Ultra serisi

Intel, yapay zekayı masaüstüne getiriyor! İşte yeni Core Ultra serisi

Intel geçtiğimiz aylarda tanıttığı Core Ultra işlemcileriyle yeni bir neslin önünü açtı. Bu seriyle birlikte yapay zeka dönemini de başlatan...

Spread the love <3
Whatsapp İletişim
Merhaba,
Size nasıl yardımcı olabilirim ?