How to Fix the “TypeError: Cannot Concatenate Str And Int Objects” in Python?

Do you face the “TypeError: cannot concatenate ‘str’ and ‘int’ objects” or the “TypeError: can only concatenate str (not “int”) to str” error while concatenating or adding a string and integer value in Python?

The “TypeError: cannot concatenate ‘str’ and ‘int’ objects” is a common error in Python that occurs when you try to concatenate a string and an integer. In this article, we will show you how to fix this error and why it occurs in the first place.

Python does not support adding or concatenating a string and an integer value because these two have completely different data types. If you take the user’s input, a string, you can later type cast accordingly.

That being said, in this article, we will show you how to fix this error and why it occurs in the first place.

 

What Causes the “TypeError: Cannot Concatenate ‘Str’ And ‘Int’ Objects” Error?

The “TypeError: cannot concatenate ‘str’ and ‘int’ objects” error occurs when you try to concatenate a string and an integer using the + operator. In Python, the + operator is used for concatenation only if both operands are strings. If one of the operands is an integer, Python will attempt to perform arithmetic addition instead of string concatenation.

For example, let’s say you want to calculate the commission-based salary of an employee at your company; there would be a defined base salary plus a variable commission rate. For further understanding, let’s have a look at the below code:

 

Code

salary = 1500

commission_rate = input("Enter commission rate: ")

print("The data type of salary is\t\t",type(salary))

print("The data type of commission rate is\t",type(commission_rate))

print(f"Your base salary is {salary}, and your commission rate is {commission_rate}")

print("Total salary for this month is ", salary+commission_rate)

 

Output

Enter commission rate: 3.3

The data type of salary is  <class 'int'>

The data type of commission rate is  <class 'str'>

Your base salary is 1500, and your commission rate is 3.3

Cannot concatenate str and int objects

 

The above ☝️ program has raised a TypeError saying that you cannot add an integer and string because these two belong to different classes and their data types aren’t the same. 

If you are not taking any user input but just adding two values and getting the TypeError can only concatenate str (not “int”) to str as follows, you probably must be doing something wrong with the values.

 

Code

string = "string"

number = 24

print(string+number)

 

Output

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

 

 

How to Fix the “TypeError: Cannot Concatenate Str And Int Objects” Error?

We have seen in the above examples that we cannot add a string and an integer and the error message itself is self-explanatory. But wait, how do we resolve it then, because the `input function always gets every input as a string?

 

Method 1: Type Casting the Variables

Thanks to Python,  it provides us with different approaches to converting strings into integers and vice versa. Let’s have a look at the same example of the employee salary:

 

Code

salary = 1500

commission_rate = float(input("Enter commission rate: ")) # type casted to int

print(f"Your base salary is{salary} and your commission rate is {commission_rate}")

print("The Total salary for this month is", salary+commission_rate)

 

Output

Enter commission rate: 3.4

Your base salary is1500 and your commission rate is 3.4

The total salary for this month is 1503.4

 

You can see in this statement commission_rate = float(input(“Enter commission rate:”)), we have used the float function that converts a string into a float. And float belongs to the integer class, so adding two integers isn’t a problem.

Similarly, you must make sure both belong to the string class to concatenate two strings.

 

Code

string = "String"

number = 24

# type cast number into a string

num_str = str(number)

print(string+num_str)

 

Output

String24

 

In the above example, we first converted a number into a string as num_str and then concatenated it with a string. The idea here is you can only join lines, not other data types, because they have different mathematical properties that don’t allow concatenation.

 

 

Method 2: String Interpolation to Concatenate the Variables

Alternatively, you can use string interpolation to concatenate the variables. This method uses placeholders in a string to insert values from variables. For example, the following code will fix the error by using string interpolation to insert the age variable into the string:

 

Code

name = "John"

age = 34

# Use string interpolation to concatenate the name and age

full_name = f"{name} is {age} years old."

 

Output

John is 24 years old.

 

In this code, we use the f string syntax to create a string with placeholders for the name and age variables. We use the {} syntax to insert the values of these variables into the string. This allows us to concatenate the name and age variables without causing an error.

 

 

Conclusion

To conclude, yhe “TypeError: cannot concatenate ‘str’ and ‘int’ objects” error occurs when you try to concatenate a string and an integer in Python. To fix this error, you need to convert the integer to a string before concatenating it with the string, or you can perform string interpolation.

This article’s key takeaway is to understand how you can fix the “TypeError: cannot concatenate str and int objects” that you might encounter in Python. We have discussed why this error occurs and how to typecast the data types in Python to avoid this TypeError and add or concatenate two values.

To summarize the article, let’s quickly recap the topics discussed in the report.

  1. What is the TypeError can only concatenate str (not “int”) to str in Python?
  2. How to fix the TypeError Cannot Concatenate Str And Int Objects?
  3. Type Casting in Python.
  4. String Interpolation to Concatenate the Variables in Python.

It’s time to explore more, and name any function used to concatenate two strings in Python other than the plus ‘+’ operator.

Total
0
Shares
Leave a Reply

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

Related Posts
Total
0
Share