Python TypeError: Can Only Concatenate str (Not “Bytes”) to str

I have seen this error pop up more times than I can count. It usually happens when you are working with file systems, network sockets, or external APIs.

The “can’t concat str to bytes” error is a classic hurdle for anyone moving from Python 2 to Python 3.

In this guide, I will share my experience on why this happens and how you can fix it quickly.

Why Does This Error Occur in Python?

In Python 3, there is a very strict wall between text and raw data. Strings (str) are meant for human-readable text, while bytes are meant for raw binary data.

Python refuses to “guess” how you want to combine them, so it throws a TypeError if you try to add them together with a + operator.

Method 1: Convert Bytes to String Using decode()

The most common way I fix this is by converting the bytes object into a string. I do this using the .decode() method, which tells Python how to interpret the binary data.

Here is a practical example using a US-based weather station data log:

# Simulated raw binary data from a weather station in Chicago
raw_temperature_data = b"Current Temp: 32F"
suffix = " - Data Verified by NWS"

# Attempting to concatenate directly will fail:
# result = raw_temperature_data + suffix (This raises TypeError)

# The proper way: Decode the bytes to a string
decoded_temp = raw_temperature_data.decode("utf-8")
final_report = decoded_temp + suffix

print(final_report)
# Output: Current Temp: 32F - Data Verified by NWS

You can refer to the screenshot below to see the output.

TypeError Can Only Concatenate str (Not Bytes) to str Python

I always recommend using “utf-8” as the encoding because it handles almost everything you’ll encounter.

Method 2: Convert String to Bytes Using encode()

Sometimes, you need the final result to be in bytes, especially if you are sending data to a server.

In this case, I convert the string part into bytes using the .encode() method.

Imagine you are sending a command to a US financial database:

# Database command in bytes
db_command = b"FETCH_ACCOUNT_ID:"
account_number = "US-998877"

# Convert the account number string to bytes
account_bytes = account_number.encode("utf-8")

# Now concatenate bytes to bytes
full_request = db_command + account_bytes

print(full_request)
# Output: b'FETCH_ACCOUNT_ID:US-998877'

You can refer to the screenshot below to see the output.

Python TypeError Can Only Concatenate str (Not Bytes) to str in Python

This ensures that both objects are of the same type before the operation.

Method 3: Use Byte Literals (The ‘b’ Prefix)

If you are hardcoding a string and you know it needs to be bytes, the easiest fix is to add a b prefix.

I use this a lot for quick debugging or when defining constants for network protocols.

# Wrong way: Mixing str and bytes
# message = "Order_Total: " + b"$150.00"

# Right way: Use the 'b' prefix for the label as well
message = b"Order_Total: " + b"$150.00"

print(message)
# Output: b'Order_Total: $150.00'

You can refer to the screenshot below to see the output.

TypeError Can Only Concatenate str (Not Bytes) to str

This is the fastest solution when you don’t need to handle dynamic variables.

Method 4: Fix the Issue in File Handling

I often see junior developers run into this when writing to files in the wrong mode.

If you open a file in “wb” (write binary) mode, you must write bytes, not strings.

# Writing shipping logs for a New York warehouse
log_entry = "Shipment #5501 - Shipped via FedEx\n"

# Incorrect: with open("logs.bin", "wb") as f: f.write(log_entry)

# Correct: Encode the string before writing to a binary file
with open("shipping_logs.bin", "wb") as f:
    f.write(log_entry.encode("utf-8"))

Alternatively, if you want to write text, just change the mode to “w”.

Method 5: Use f-strings with Decoding

If you like f-strings as much as I do, you can still use them by decoding the bytes inside the brackets.

This is a very clean way to format messages for US-based logging systems.

server_status_bytes = b"ONLINE"
location = "Dallas, TX"

# Decode inside the f-string to keep it human-readable
formatted_status = f"Server in {location} is currently {server_status_bytes.decode()}"

print(formatted_status)
# Output: Server in Dallas, TX is currently ONLINE

This method is my personal favorite for maintaining readability in larger projects.

I hope you found this tutorial helpful! Handling the “can’t concat str to bytes” error is really just about being mindful of your data types.

In this article, I have covered the most common scenarios and fixes that I have used throughout my career. If you are dealing with web scraping or network programming, you will likely use Method 1 or 2 most often.

Just remember to always match your types before using the + operator, and you will avoid this error every time.

You may read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.