Fix Python TypeError: can only concatenate str (not "int") to str

Python shows TypeError: can only concatenate str (not "int") to str when you try to concatenate (merge) a string and an integer using the + operator, which is not allowed in Python.

To fix this error, you need to either convert the int to string with str(), or the string to int with int().

Let’s see how to convert Python integer to string first.

Convert an integer to string

Suppose you run the Python code below:

price = 50

# ❌ TypeError: can only concatenate str (not "int") to str
print("The price is " + price + " dollars")

Because the price variable contains an int data type, concatenating it with strings as shown above produces an error:

Traceback (most recent call last):
  File ...
    print("The price is " + price + " dollars")
TypeError: can only concatenate str (not "int") to str

To fix this error, you need to convert the price variable to string when printing the value. There are four ways you can do this in Python:

  1. Use the str() function.
  2. Use f-string to create a formatted string
  3. Call the format() function
  4. Use commas in the print statement

See the example below:

price = 50

# Use str() function to print
print("The price is " + str(price) + " dollars")

# use f-string to insert integer value into string
print(f"The price is {price} dollars")

# call the format() function
print("The price is {} dollars".format(price))

# use commas to merge many values as one
print("The price is", price ,"dollars")

The first solution is to convert the price variable to string manually with the str() function.

The second and third solution is to create a formatted string. They have different syntaxes but produce the same result.

The last solution is to pass the values you want to merge as multiple arguments. The print() function automatically converts the price variable into string with this.

Convert a string to integer

In another case, you may have a string when you want an integer value.

Receiving user input using the input() function always returns a string even when you asked for a number like this:

price = 50

# enter shipping cost
shipping_cost = input("Input shipping cost amount: ")

# ❌ TypeError: can only concatenate str (not "int") to str
total = shipping_cost + price

When you add the shipping_cost and price values, Python responds with the error message because the input() function always returns a string.

You need to convert the value to integer using the int() function as follows:

price = 50

# enter shipping cost
shipping_cost = input("Input shipping cost amount: ")

total = int(shipping_cost) + price  # ✅

Once you convert the string to an integer, Python will be able to add the two operands together.

Conclusion

Python responds with TypeError: can only concatenate str (not "int") to str because it doesn’t allow concatenation between an integer value with a string value.

The solution is to either convert the string to integer, or the integer to string as shown in this article.

Take your skills to the next level ⚡️

I'm sending out an occasional email with the latest tutorials on programming, web development, and statistics. Drop your email in the box below and I'll send new stuff straight into your inbox!

No spam. Unsubscribe anytime.