How To Resolve TypeError: Can Only Concatenate Str (Not “Bytes”) To Str in Python

TypeError: can only concatenate str (not “bytes”) to str

Concatenating a string with a Byte object is not possible in Python, it gives an error “TypeError: can only concatenate str (not “bytes”) to str”. Read the following article to find out how to fix it.

What causes the error “TypeError: can only concatenate str (not “bytes”) to str”?

In Python 2, both string and type are the same object in typebyte. But since Python 3, the definition of ‘Byte’ is string bytes like Unicode objects in Python 2. String and Byte have the following differences:

  • The ‘Byte’ object is defined as a byte string, and the ‘string’ is defined as a character string.
  • The ‘Byte’ object is created to be understood by computers, ‘string’ is created to be readable by humans.
  • The ‘Byte’ object can be stored directly on disk, and the ‘string’ object needs to be encoded by the computer to be understood and then stored on disk.

TypeError: Python throws this error when you perform an unsupported operation on an object with an invalid data type.

“TypeError: can only concatenate str (not “bytes”) to str” happens when you concatenate a string with a byte object. As I have shown the definition and difference between the two objects above, it is impossible to join them.

Example: 

myStr = 'visit'
byteStr = b'LEARNSHAREIT website'

print(myStr + byteStr)

Output:

Traceback (most recent call last):
  File "code.py", line 4, in <module>
    print(myStr + byteStr)
TypeError: can only concatenate str (not "bytes") to str

Note: I use the ‘+’ operator to concatenate two objects.

How to solve this error?

Use str() function

You can use the str() function to cast the byte object type to a string with the following syntax:

str()

Example:

  • Create a string and an object.
  • The str() function casts the byte object type and uses the ‘+’ operator to concatenate them.
myStr = 'visit'
byteStr = b'LEARNSHAREIT website'

# Use str() function to convert byte object to string object
newStr = str(byteStr , 'UTF-8')
print(myStr + newStr)

Output:

visitLEARNSHAREIT website

Use map() function

The map() function can convert byte objects to strings. The map() function traverses the elements of an iterable through a given function and returns a list of results. I will use chr() as the given function.

Syntax:

map(func,iterable)

Parameters:

  • func: given function to iterate over the elements of iterable.
  • iterable: the object you want to browse.

Example:

  • Create a string and an object.
  • The map() function converts the byte object type and uses the ‘+’ operator to concatenate them.
myStr = 'visit'
byteStr = b'LEARNSHAREIT website'

# Use map() function to convert byte object to string object
newStr = ''.join(map(chr, byteStr))
print(myStr + newStr)

Output:

visitLEARNSHAREIT website

String encoding

According to the definition I have given, the string is made to be read by humans, so for computers to understand it, we need to encode it. It would be best if you used the ‘utf-8’ encoder (more than a million characters encoding) for smooth encoding.

Example:

  • Create a string.
  • Create a byte object.
  • Encode the string to byte object and use the ‘+’ operator to concatenate two objects.
myStr = 'visit'
byteStr = b'LEARNSHAREIT website'

# Encode string object
newStr = myStr.encode('utf-8')
print(newStr + byteStr)

Output:

b'visitLEARNSHAREIT website'

Byte object decode

Same as string encoding, this time, I use byte object decoding. You should still use the ‘utf-8’ encoder.

Example:

  • Create a string.
  • Create a byte object.
  • Decode the byte object to string and use the ‘+’ operator to concatenate the two objects.
myStr = 'visit'
byteStr = b'LEARNSHAREIT website'

# Decode byte object
newStr = byteStr.decode('utf-8')
print(myStr + newStr)

Output:

visitLEARNSHAREIT website

Summary

If you have any questions about the “TypeError: can only concatenate str (not “bytes”) to str” in Python, leave a comment below. I will answer your questions. Thanks for reading!

Maybe you are interested:

Leave a Reply

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