Solve TypeError: Cannot Concatenate STR and Int Objects in Python

Solve TypeError: Cannot Concatenate STR and Int Objects in Python

  1. Solve TypeError: cannot concatenate 'str' and 'int' objects in Python
  2. Ways to Avoid the TypeError: cannot concatenate 'str' and 'int' objects Error in Python
  3. Conclusion

While coding in any programming language, we encounter certain unexpected situations that cause abnormal behavior in the program. These unexpected situations are known as errors.

In this article, we will discuss one error known as the TypeError: cannot concatenate 'str' and 'int' objects error in Python.

Solve TypeError: cannot concatenate 'str' and 'int' objects in Python

Errors are unexpected and illegal operations performed by a user that results in the unusual working of a program or sometimes even stop the program until this error is resolved. There are various types of errors - Logical, Runtime, and Syntax.

The logical error occurs when there is a problem in the logic of the program. On the other hand, a runtime error occurs when executing the program after successful compilation, whereas syntax errors occur when the program is syntactically incorrect.

The TypeError: cannot concatenate 'str' and 'int' objects in Python is a type of runtime error when executing the program. An example of a runtime error can be dividing a number by zero.

However, the TypeError: cannot concatenate 'str' and 'int' objects in Python deals with the issue caused by the type of the variables used in the program. It often occurs when the values we are concatenating are of different types; you can only combine values of the same type.

Let’s take the examples: a= 2+3 and s= "Rohan"+"Roshni". The variables a and s add/concatenate the same types of values.

However, if you try to add a string object with an integer object, it would give us an error known as TypeError: cannot concatenate 'str' and 'int' objects in Python. We can not concatenate values of different objects since Python does not support the auto type of the variable to cast.

Let us see the example when the TypeError: cannot concatenate 'str' and 'int' objects error occurs in Python.

x = "Rohan" + 2;
print(x)

Output:

Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    x = "Rohan" + 2
TypeError: can only concatenate str (not "int") to str

Therefore, as you can see in the above output, it gives us the TypeError stating that it can only concatenate str (not "int") to str. Therefore, the string object must only be concatenated with a string object, whereas an integer object must only be added with an integer object.

However, there are situations where we have to concatenate a string and an integer object.

For example,

a = 10
print("The value of:" + a)

Therefore, if you want to print a message along with a value, you must concatenate a string and an integer object.

Ways to Avoid the TypeError: cannot concatenate 'str' and 'int' objects Error in Python

To deal with such situations where we have to concatenate a string and an integer object, we have several ways to avoid this error. Therefore, we will now understand how to resolve the TypeError: cannot concatenate 'str' and 'int' objects error in Python.

Use the In-Built str() Function in Python

Python has an in-built function str() that converts an integer value to a string. However, this str() function takes an integer value as an argument and returns a string value to the user.

Therefore, we can convert the integer value so that it is cast and can be concatenated with a string value. Let us take an example to show the error that we counter and then the solution using the str() function.

a = 10
print("The value =:" + a)

Output:

Traceback (most recent call last):
  File "<pyshell#6>", line 1, in <module>
    print("The value =:" + a)
TypeError: can only concatenate str (not "int") to str

Now, let us see the solution to the above problem.

a = 10
print("The value =:" + str(a))

Output:

The value =:10

As you can see in the solution code, the variable a is converted to a string value using the str() function in Python to avoid the TypeError: cannot concatenate 'str' and 'int' objects error.

Use Comma While Using the print Statement in Python

The print statement in Python can also be passed with values of different data types. However, all the values passed to the print statement are internally converted to a string data type and printed.

Let us a code to understand it better in Python.

a = 10
print("The value of a=" + a)

Output:

Traceback (most recent call last):
  File "<pyshell#7>", line 1, in <module>
    print("The value of a=" + a)
TypeError: can only concatenate str (not "int") to str

Solution:

a = 10
print("The value of a=", a)

Output:

The value of a= 10

As you can see in the solution code, replacing the + concatenation symbol with a , (comma) resolved the TypeError: cannot concatenate 'str' and 'int' objects error in Python.

Use the format() Function in Python

We can also resolve the TypeError: cannot concatenate 'str' and 'int' objects error by using Python’s format() function. The format() function is an in-built method used for variable substitution and data formatting.

This function lets you concatenate the string parts at particular positions through placeholders. These placeholders are represented by a pair of curly braces {}.

You must use the placeholder where you want to concatenate the other value and call the str.format() method after it.

However, the format() method takes the value that needs to be concatenated. It can be of any data type, integer, float, or string.

Let us see an example to understand it better.

a=10
print("The value of a = {}".format(a))

Output:

The value of a = 10

Therefore, as you can see, the value of a when passed to the format() function gets concatenated at the placeholder’s position and doesn’t cause the TypeError: cannot concatenate 'str' and 'int' objects error in our code.

Therefore, these were the three ways we can resolve the TypeError: cannot concatenate 'str' and 'int' objects error we encountered while concatenating with different data types in Python.

Conclusion

In this article, we have discussed the TypeError: cannot concatenate 'str' and 'int' objects error in Python. This TypeError often occurs when we combine the values of different data types, such as an integer and string object concatenated together, giving a TypeError: cannot concatenate 'str' and 'int' objects in Python.

It mainly occurs because Python does not support the auto type of variable for casting.

However, we have discussed three ways this TypeError gets resolved. The first uses the in-built str() method in Python, and the second uses the in-built format() method in Python. The third way the TypeError can be resolved is by using Python’s print statement.

However, the most common and easiest way to resolve this error would be using Python’s str() method.

Related Article - Python Error

  • Local Variable Referenced Before Assignment Error in Python
  • Python Handling Socket.Error: [Errno 104] Connection Reset by Peer
  • Python Is Not Recognized in Windows 10
  • NameError: The OS Module Is Not Defined in Python
  • Python ValueError: Not Enough Values to Unpack
  • Resolve the TypeError: Can't Multiply Sequence by Non-Int of Type STR in Python