close

[Solved] TypeError: Can only concatenate str (not “int”) to str (simple Python programme)

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error TypeError: Can only concatenate str (not “int”) to str (simple Python programme) in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How TypeError: Can only concatenate str (not “int”) to str (simple Python programme) Error Occurs?

Today I get the following error TypeError: Can only concatenate str (not “int”) to str (simple Python programme) in python.

How To Solve TypeError: Can only concatenate str (not “int”) to str (simple Python programme) Error ?

  1. How To Solve TypeError: Can only concatenate str (not “int”) to str (simple Python programme) Error ?

    To Solve TypeError: Can only concatenate str (not “int”) to str (simple Python programme) Error This Error states that it can only concatenate string to string. In your program you have tried to concatenate a sting and integer

  2. TypeError: Can only concatenate str (not “int”) to str (simple Python programme)

    To Solve TypeError: Can only concatenate str (not “int”) to str (simple Python programme) Error This Error states that it can only concatenate string to string. In your program you have tried to concatenate a sting and integer

Solution 1

The input() command in line 2 of your code would turn any input provided by the user into a STRING. Therefore, when you try to add that STRING to a number (float or integer; in your case you have an integer i.e. factor=2) it won’t (and shouldn’t!) work.

Therefore, for the + operation to continue, both the quantities to the left and right of that + sign must be of the same type (strings, or numbers)

Solution 2

TypeError: Can only concatenate str (not “int”) to str

This Error states that it can only concatenate string to string. In your program you have tried to concatenate a sting and integer

Input command would only fetch string from the user, and the age the user enters should be taken as string. Examples: ’45’, ’25’, 36 etc..

This program is trying to concatenate ’45’ + 2 . Which throws this error.

Instead you can try converting the user input to int and then concatenate.

userName = input('Name: ')
age = int(input('age: '))
finalAge = age + factor
factor = 2
finalAge = age + factor
print('In ', factor, 'years you will be', finalAge, 'years old', userName, '!')

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read