Python TypeError: can only concatenate str (not “int”) to str Solution

Posted in /  

Python TypeError: can only concatenate str (not “int”) to str Solution
vinaykhatri

Vinay Khatri
Last updated on April 20, 2024

    In Python, we can use the + symbol as an addition operator between two numeric values and a concatenation operator between two string values. But if we try to put the + operator between a string value and a numeric value, we will encounter the TypeError: can only concatenate str (not "int") to str Error.

    Here, in this Python guide, we will explore the TypeError: can only concatenate str (not "int") to str Error, and discuss how to solve it. We will also discuss an example to show you how this error occurs in Python and how to debug it. So let's get started with the Error Statement.

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

    The Error statement TypeError: can only concatenate str (not "int") to str has two parts.

    1. Exception Type = TypeError
    2. Error Message = can only concatenate str (not "int") to str

    1. Exception Type ( TypeError )

    TypeError is a Python standard exception, and it occurs when we perform an invalid operation on a Python data type object . Performing the mathematical operation between a string and an integer value is a classic TypeError in Python .

    2. Error Message ( can only concatenate str (not "int") to str )

    The Error Message can only concatenate str (not "int") to str is telling us that we are using an + operator between a string and int value. Actually, the message is signaling that we can only perform the concatenation operation between two string values using an + operator, and in the program, we are using the integer value as the second operand.

    Example

    string = "23"
    integer = 24
    
    # error
    result = string + integer
    
    print(result)

    Output

    Traceback (most recent call last):
    File "main.py", line 5, in <module>
    result = string + integer
    TypeError: can only concatenate str (not "int") to str

    Break the code

    In the above example, we are getting the error in line 5 with result = string + integer statement. The reason behind this error is very straightforward. When the Python interpreter tries to execute line 5, it starts from left to right. It parses the string value first, then the + operator, and based on the first string value, it interprets the + operator as a concatenation operator. When it found out that the integer is an int object, it could not perform the concatenation operation and threw the error.

    Solution

    There could be two solutions to this problem, we can either convert the string value to int or float using int() or float() functions and perform the addition operation between the two operands, OR. We can convert the integer to str object and perform the concatenation operation between the two operands.

    Example

    string = "23"
    integer = 24
    
    # concatenate
    concatenate = string + str(integer)
    # addition
    add = int(string) + integer
    
    print("Concatenation value:", concatenate )
    print("Addition value:", add)

    Output

    Concatination value: 2324
    Addition value: 47

    Common Error Scenario

    Many new Python learners encounter this error when they try to concatenate a string with an integer value in the print statement.

    rank1 ={
            'Name':'Rahul',
            'Score': 499,
            'Age'  : 20
            }
    
    print(rank1['Name']+" has scored "+ rank1['Score'])

    Output

    Traceback (most recent call last):
    File "main.py", line 7, in <module>
    print(rank1['Name']+"has scored "+ rank1['Score'])
    TypeError: can only concatenate str (not "int") to str

    Break the code

    In the above example, we are receiving the error in line 7 with the print() statement. Inside the print statement, we are using the + symbol as a concatenation operator to concatenate the rank1 name with some message and  the Score .

    The error was raised because the value of rank1['Score'] is 499 which is an integer value, and the data before that is all string. And we know that when we try to concatenate a string value with an integer value, we get the Error.

    Solution

    We can see that all we are trying to do is concatenate the complete message inside the print statement. And when we want to concatenate an integer value with a string value, we need to convert the integer value to a string using the str() function.

    To solve the above problem, all we need to do is convert the value of rank1['Score'] to string using a str() function.

    Example Solution

    rank1 ={
            'Name':'Rahul',
            'Score': 499,
            'Age'  : 20
            }
    
    print(rank1['Name']+" has scored "+ str(rank1['Score']))

    Output

    Rahul has scored 499

    Final Thoughts!

    The can only concatenate str (not "float") to str is a TypeError, which occurs in Python when we try to concatenate a string value with an integer number. To debug this problem all we need to do is convert the integer value to a string value using the Python str() function. This is a very common error and can be solved with ease.

    If still, you are getting this error in your python program, you can share your code in the comment section. We will try to help you in debugging.

    People are also reading:

    Leave a Comment on this Post

    0 Comments