TypeError: String Argument Without An Encoding In Python

TypeError: string argument without an encoding in Python

When the error TypeError: string argument without an encoding in Python appears, it annoys you. We’re here to help you find out the solutions to fix it. Follow this article to know how to handle it.

When does TypeError: String Argument Without An Encoding In Python error occur?

TypeError: string argument without an encoding will occur when we do not correctly use bytes() or bytearray() function. Check the code below.

Code example:

print(bytes("Learn Share IT"))
print(bytearray("Learn Share IT"))

Output:

TypeError: string argument without an encoding

So we have found out the cause of the error, and now we will go to the solutions for this error.

How to fix this error?

Using the encoding UTF-8

If you don’t want to get this error when using the byte() or bytearray() function, you must specify your string using ‘utf-8’ because all strings in Python are Unicode, and byte is binary data.

Syntax:

bytes(source, encoding)

Parameters:

  • source: an integer or a string
  • encoding: the encoding of the string

Returns: an object

So, we use encoding ‘utf-8‘ to pass this error simply. 

Firstly, we initialize a variable containing the string to be converted. Then, we use the bytes() function to convert string to bytes object and print to console. See the code below.

Code example:

# Initialize a variable store string
my_string = "Learn Share IT"

# Convert string to byte
convert = bytes(my_string, encoding="utf-8")

print(convert)

Output:

b'Learn Share IT'

Besides, we can also use the bytearray() function to fix this error.

Syntax:

bytearray(source, encoding)

Parameters:

  • source: an integer, a string, or an object.
  • encoding: the encoding of the string.

Return: bytearray object that is an integer or string

Code example:

# No argument
convert1 = bytearray()
print(convert1)

# Integer argument
convert2 = bytearray(3)
print(convert2)

# List argument
convert3 = bytearray([1, 2, 3])
print(convert3)

# String argument
convert4 = bytearray("Learn Share IT ", "utf-8")
print(convert4)

Output:

bytearray(b'')
bytearray(b'\x00\x00\x00')
bytearray(b'\x01\x02\x03')
bytearray(b'Learn Share IT ')

Using the encode() method

We can use the encode() method to convert a string to byte object.

Firstly, we need a variable to store a string. Then, we use the encode(“utf-8”) function to convert string to byte object and print. Follow code example.

Code example:

# Initialize a variable store string
my_string = "Learn Share IT"

# Convert string to byte
convert = my_string.encode('utf-8')

print(convert)

Output:

b'Learn Share IT'

Summary

That’s all for today. We hope the ways to fix the error TypeError: string argument without an encoding in Python that we mentioned above are helpful to you. Thank you for reading!

Have a beautiful day!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *