- How to convert Python string to bytes?
- Table of Contents — Python String to Byte:
- Python String to Bytes:
- Method to convert strings to bytes:
- Using bytes():
- Using encode():
- Limitations and Caveats — Python String to Byte
- Python str to bytes without encode
- # TypeError: string argument without an encoding in Python
- # Specify the encoding in the call to the bytes() class
- # Using the str.encode() method to convert a string to bytes
- # Using the bytes.decode() method to convert a bytes object to a string
- # Using the bytes() and str() classes instead
- # Additional Resources
- Python String to bytes, bytes to String
- Python String to bytes
- 1. Python String to bytes using bytes() method
- 2. Python String to bytes using encode() method
- Python bytes to String
- Pandas bytes to String
- Conclusion
- References
How to convert Python string to bytes?
In this tutorial, we look at how to convert Python string to bytes. We look at all the various methods along with their limitations and caveats.
Table of Contents — Python String to Byte:
Python String to Bytes:
Converting Python strings to Bytes has become quite popular after the release of Python 3. This is largely because a lot of file handling and Machine learning methods require you to convert them. Before we dive into how to convert them let us first understand what they are and how they are different.
In Python 2, string and bytes were the same typeByte objects; however after the introduction of Python 3 Byte objects are considered as a sequence of bytes, and strings are considered as a sequence of characters. In essence, strings are human-readable and in order for them to become machine-readable, they must be converted to byte objects. This conversion also enables the data to be directly stored on the disk.
The process of converting string objects to byte objects is called encoding and the inverse is called decoding. We look at methods to achieve this below.
Method to convert strings to bytes:
There are many methods that can be used to convert Python string to bytes, however, we look at the most common and simple methods that can be used.
Using bytes():
The bytes() method is an inbuilt function that can be used to convert objects to byte objects.
The bytes take in an object (a string in our case), the required encoding method, and convert it into a byte object. The bytes() method accepts a third argument on how to handle errors.
Let us look at the code to convert a Python string to bytes. The encoding type we use here is “UTF-8”.
#Using the byte() method # initializing string str_1 = "Join our freelance network" str_1_encoded = bytes(str_1,'UTF-8') #printing the encode string print(str_1_encoded) #printing individual bytes for bytes in str_1_encoded: print(bytes, end = ' ')
b'Join our freelance network' 74 111 105 110 32 111 117 114 32 102 114 101 101 108 97 110 99 101 32 110 101 116 119 111 114 107
As you can see this method has converted the string into a sequence of bytes.
Note: This method converts objects into immutable bytes, if you are looking for a mutable method you can use the bytearray() method.
Using encode():
The encode() method is the most commonly used and recommended method to convert Python strings to bytes. A major reason is that it is more readable.
string.encode(encoding=encoding, errors=errors)
Here, string refers to the string you are looking to convert.
- Encoding — Optional. The encoding method you are looking to use. After Python 3, UTF-8 has become the default.
- Error — Optional, A string containing the error message.
#Using the encode method # initializing string str_1 = "Join our freelance network" str_1_encoded = str_1.encode(encoding = 'UTF-8') #printing the encode string print(str_1_encoded) #printing individual bytes for bytes in str_1_encoded: print(bytes, end = ' ')
b'Join our freelance network' 74 111 105 110 32 111 117 114 32 102 114 101 101 108 97 110 99 101 32 110 101 116 119 111 114 107
#Using the encode method #initializing string str_1 = "Join our freelance network" str_1_encoded = str_1.encode(encoding = 'UTF-8') #printing the encode string print(str_1_encoded) #decoding the string str_1_decoded = str_1_encoded.decode() print(str_1_decoded)
b'Join our freelance network' Join our freelance network
Limitations and Caveats — Python String to Byte
- Both the methods solve the same problem efficiently and choosing a particular method would boil down to one’s personal choice. However, I would recommend the second method for beginners.
- The byte() method returns an immutable object. Hence, consider using the bytearray() if you are looking for a mutable object.
- While using the byte() methods the object must have a size 0
Python str to bytes without encode
Last updated: Feb 2, 2023
Reading time · 3 min
# TypeError: string argument without an encoding in Python
The Python «TypeError: string argument without an encoding» occurs when we pass a string to the bytes class without specifying the encoding.
To solve the error, pass the encoding as the second argument to the bytes class.
Here are 2 examples of how the error occurs when using the bytes and bytearray classes.
Copied!# ⛔️ TypeError: string argument without an encoding print(bytes('hello')) # ⛔️ TypeError: string argument without an encoding print(bytearray('hello'))
We got the error because we passed a string to the bytes() class without specifying the encoding.
# Specify the encoding in the call to the bytes() class
We can pass the encoding as the second argument to the class.
Copied!# 👇️ b'hello' print(bytes('hello', encoding='utf-8')) # 👇️ bytearray(b'hello') print(bytearray('hello', encoding='utf-8'))
When a string is passed to the bytes or bytearray classes, we must also specify the encoding.
You can also pass the encoding as a positional argument to the bytes and bytearray classes.
Copied!# 👇️ b'hello' print(bytes('hello', 'utf-8')) # 👇️ bytearray(b'hello') print(bytearray('hello', 'utf-8'))
Even though it is obvious that the second parameter is the encoding, it is still a bit implicit and more difficult to read.
The bytes class returns a new bytes object which is an immutable sequence of integers in the range 0
The bytearray class returns an array of bytes and is a mutable sequence of integers in the same range.
# Using the str.encode() method to convert a string to bytes
You can also use the str.encode method to convert a string to a bytes object.
Copied!my_str = 'hello' my_bytes = my_str.encode('utf-8') print(my_bytes) # 👉️ b'hello'
The str.encode method returns an encoded version of the string as a bytes object. The default encoding is utf-8 .
# Using the bytes.decode() method to convert a bytes object to a string
Conversely, you can use the decode() method to convert a bytes object to a string.
Copied!my_str = 'hello' my_bytes = my_str.encode('utf-8') print(my_bytes) # 👉️ b'hello' my_str_again = my_bytes.decode('utf-8') print(my_str_again) # 👉️ 'hello'
The bytes.decode method returns a string decoded from the given bytes. The default encoding is utf-8 .
Encoding is the process of converting a string to a bytes object and decoding is the process of converting a bytes object to a string .
In other words, you can use the str.encode() method to go from str to bytes and bytes.decode() to go from bytes to str .
# Using the bytes() and str() classes instead
You can also use bytes(s, encoding=. ) and str(b, encoding=. ) .
Copied!my_text = 'hello' my_binary_data = bytes(my_text, encoding='utf-8') print(my_binary_data) # 👉️ b'hello' my_text_again = str(my_binary_data, encoding='utf-8') print(my_text_again) # 👉️ 'hello'
The str class returns a string version of the given object. If an object is not provided, the class returns an empty string.
Ever since Python 3, the language uses the concepts of text and binary data instead of Unicode strings and 8-bit strings.
All text in Python is Unicode, however, encoded Unicode is represented as binary data.
You can use the str type to store text and the bytes type to store binary data.
In Python 3 you can no longer use u’. ‘ literals for Unicode text because all strings are now Unicode.
However, you must use b’. ‘ literals for binary data.
When the first argument we pass to the bytes() or bytearray() classes is a string, you must also specify the encoding.
You only have to specify the encoding when the first argument you pass to the methods is a string.
If you call the bytearray() method with an integer or an iterable of integers, you don’t have to specify the encoding.
Copied!print(bytearray(5)) # 👉️ bytearray(b'\x00\x00\x00\x00\x00') print(bytearray([1, 2, 3])) # 👉️ bytearray(b'\x01\x02\x03')
# Additional Resources
You can learn more about the related topics by checking out the following tutorials:
I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
Python String to bytes, bytes to String
In this article, we will have a look at the conversion of Python String to bytes and Python bytes to String. Python conversion of type has gained quite an importance due to its feature of data being used during various operations in a different form.
Python conversion of String to bytes and bytes to String has its own importance for the fact that it is necessary while file handling, etc.
Python String to bytes
Either of the following ways can be used to convert Python String to bytes:
1. Python String to bytes using bytes() method
Python’s CPython library provides us with bytes() function to convert String to bytes.
Note: The UTF-8 format is used for the purpose of encoding.
inp = "Engineering Discipline" print("Input String:\n") print(str(inp)) opt = bytes(inp, 'utf-8') print("String after getting converted to bytes:\n") print(str(opt)) print(str(type(opt)))
Input String: Engineering Discipline String after getting converted to bytes: b’Engineering Discipline’
2. Python String to bytes using encode() method
Python’s encode() method can also be used to convert a String to byte format.
inp = "Engineering Discipline" print("Input String:\n") print(str(inp)) opt = inp.encode('utf-8') print("String after getting converted to bytes:\n") print(str(opt)) print(str(type(opt)))
Input String: Engineering Discipline String after getting converted to bytes: b’Engineering Discipline’
Python bytes to String
Python’s byte class has built-in decode() method to convert Python bytes to String.
inp = "Engineering Discipline" print("Input String:\n") print(str(inp)) opt = inp.encode('utf-8') print("String after getting converted to bytes:\n") print(str(opt)) print(str(type(opt))) original = opt.decode('utf-8') print("The decoded String i.e. byte to converted string:\n") print(str(original))
In the above example, we have initially converted the input string to bytes using the encode() method. After which, the decode() method converts that encoded input to original string.
Input String: Engineering Discipline String after getting converted to bytes: b'Engineering Discipline' The decoded String i.e. byte to converted string: Engineering Discipline
Pandas bytes to String
Pandas module has got Series.str.decode() method to convert the encoded data i.e. the data in bytes format to String format.
input_string.decode(encoding = 'UTF-8')
import pandas inp = pandas.Series([b"b'Jim'", b"b'Jonny'", b"b'Shawn'"]) print("Encoded String:") print(inp) opt = inp.str.decode(encoding = 'UTF-8') print("\n") print("Decoded String:") print(opt)
In the above example, we assume the data to be in encoded format. Further which, manipulations are performed on the data.
Encoded String: 0 b"b'Jim'" 1 b"b'Jonny'" 2 b"b'Shawn'" dtype: object Decoded String: 0 b'Jim' 1 b'Jonny' 2 b'Shawn' dtype: object
Conclusion
In this article, we have understood the conversion of Python String to bytes and vice versa which also ponders over the concept of encoding and decoding.
References
Python String to bytes, bytes to String – JournalDev