TypeError: string argument without an encoding in Python

avatar

Borislav Hadzhiev

Last updated: Apr 20, 2022

banner

Photo from Unsplash

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 class, e.g. bytes('hello', encoding='utf-8').

typeerror string argument without an encoding

Here are 2 examples of how the error occurs when using the bytes and bytearray classes.

main.py
# ⛔️ 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.

We can pass the encoding as the second argument to the class.

main.py
# 👇️ 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 bytesarray classes, we must also specify the encoding.

The bytes class returns a new bytes object which is an immutable sequence of integers in the range 0 <= x < 256.

The bytearray class returns an array of bytes and is a mutable sequence of integers in the same range.

You can also use the str.encode method to convert a string to a bytes object.

main.py
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.

Conversely, you can use the decode() method to convert a bytes object to a string.

main.py
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.

You can also use bytes(s, encoding=...) and str(b, encoding=...).

main.py
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.

Conclusion #

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 class, e.g. bytes('hello', encoding='utf-8').

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.