In this article, we will deal with Typeerror string argument without an encoding.
We will also provide example codes of how this error occurs and solutions to help you solve the error quickly.
But before that let’s understand first what kind of error this is.
What is typeerror string argument without an encoding?
The TypeError: string argument without an encoding python error occurs when we pass a string to a function wherein it expects an encoded string.
However, the bytes class has not specified the encoding of the string.
Here is an example of how this error occurs:
# TypeError: string argument without an encoding
print(bytes('hello'))
# TypeError: string argument without an encoding
print(bytearray('hello'))
If we run this program this will out the following:
Traceback (most recent call last): File "C:\Users\Windows\PycharmProjects\pythonProject\main.py", line 2, in <module> print(bytes('hello')) TypeError: string argument without an encoding
The error occurs because the string passed to the bytes() without specifying the encoding.
In the next section, we will look at the solutions to this error.
How to fix typeerror string argument without an encoding?
Now, here are the solutions to fix the typeerror string argument without an encoding.
Solution 1: Use the str.encode() method to convert a string to bytes
To fix the error typeerror string argument without an encoding we are going to use the str.encode method to convert a string to bytes.
Wherein str.encode function returns the encoded version of the string as a bytes object.
Here is the example code:
my_str = 'Hello ITSOURCECODES'
my_bytes = my_str.encode('utf-8')
print(my_bytes)
If we run this code it will output the following:
b'Hello ITSOURCECODES'
Solution 2: Use the bytes.decode() method to convert a bytes object to a string
Another way to fix the error is to use decode() method. This is to convert a bytes object to a string.
Particularly, this bytes.decode is the one that returns a string from the given bytes. Thus the default encoding is utf-8.
my_str = 'Hello! ItSourcecode!'
my_bytes = my_str.encode('utf-8')
print(my_bytes) # b'Hello! ItSourcecode!'
my_str_again = my_bytes.decode('utf-8')
print(my_str_again) # Hello! ItSourcecode!
Output:
b'Hello! ItSourcecode!' Hello! ItSourcecode!
Note: Use the str.encode() to pass from str to bytes and use the bytes.decode() to pass from bytes to str.
Solution 3: Specify the encoding to the bytes() class
Conversely, we can solve the error string argument without an encoding through specifying the encoding in the call to byte() class.
Here is an example code:
print(bytes('Hello! ItSourcecode!', encoding='utf-8'))
print(bytearray('Hello! ItSourcecode!', encoding='utf-8'))
Output:
b'Hello! ItSourcecode!' bytearray(b'Hello! ItSourcecode!')
Additional solution
To pass another value type, particularly an integer, encoding does not have to be specified.
b_obj = bytes(10)
print(b_obj)
b_array = bytearray(20)
print(b_array)
Output:
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')
Conclusion
The TypeError: string argument without an encoding python error occurs when we pass a string to a function wherein it expects an encoded string.
To solve this error, pass the encoding value to the function as an argument.
By following the given solutions above, then it will solve the error you are getting.
Anyhow, if you are finding solutions to some errors you might encounter we also have TypeError can’t concat str to bytes.