2

I wrote this simple code and tried to execute in Windows 10 CMD ... and it gets the error message :

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

code :

userName = input('Name: ')
age = input('age: ')

factor = 2

finalAge = age + factor

print('In', factor, 'years you will be', finalAge, 'years old', userName+'!')

I am using Python 3.7.0 in Windows 10

Simon
  • 855
  • 9
  • 24
Madushk
  • 41
  • 1
  • 1
  • 2
  • 2
    Possible duplicate of [Python: TypeError: cannot concatenate 'str' and 'int' objects](https://stackoverflow.com/questions/11844072/python-typeerror-cannot-concatenate-str-and-int-objects) – Mark Dickinson Sep 07 '18 at 15:39

11 Answers11

3

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)

SA12345
  • 67
  • 3
  • 1
    Thanks you very much !!!! it solved the problem ... I am new to programming and now I understand that input() takes the value as a String and stores it in the variable 'age' – Madushk Sep 07 '18 at 16:12
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, '!')
1

Python is strongly typed, so it does not do type coercion unless you tell it to.

You get that error if you try to add a number to a string, because based on the first operand it figures you want to concatenate strings.

If you try to add a string to a number, you get “unsupported operand types” instead, but it’s the same problem.

If you want to turn a number-in-a-string into an int you can add, use int(). If you want to turn a number value into a string you can concatenate, use str().

Mohit kumar
  • 155
  • 1
  • 5
1
userName = input('Name: ')
age = input('age: ')

factor = 2

finalAge = int(age) + factor

print('In', factor, 'years you will be', finalAge, 'years old', userName+'!')
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
0

You add a string to an int, because the input() function returns a string. Use int(input('age: ')).

h h h
  • 15
  • 4
0

Convert age to int to do your maths:

finalAge = int(age) + factor

And as a bonus you could use the .format() option:

print("In {0} years you will be {1} years old, {2}!".format(factor, finalAge, userName))
Jonathan
  • 748
  • 3
  • 20
0

In python, you cannot concatenate two completely different data types.

1) 1 + 1 = 2
2) '1' + '1' = '11' (string within quotes)
3) '1' + 1 = ??

Think about it...

Well, in other programming languages like C, the integer would be converted into char (or character) and would undergo operation 2...

So, in this case, you need to explicitly cast the str (or string) data type other than into an integer (or int) using the function int().

Syntax: int('<some string>')

Example: int('7') would yield 7

So, you either need to take the input as integer or else convert the string to integer while computing finalAge:

age = int(input('age: '))

Or

finalAge = int(age) + factor
Melvin Abraham
  • 2,870
  • 5
  • 19
  • 33
0

In python, you cannot concatenate two completely different data types.

1) 1 + 1 = 2
2) '1' + '1' = '11' (string within quotes)
3) '1' + 1 = ??

Think about it...

Well, in other programming languages like C, the integer would be converted into char (or character) and would undergo operation 2...

So, in this case, you need to explicitly cast the str (or string) data type other than into an integer (or int) using the function int().

Syntax: int('<some string>')

Example: int('7') would yield 7

So, you either need to take the input as integer or else convert the string to integer while computing finalAge:

age = int(input('age: '))

Or

finalAge = int(age) + factor
Melvin Abraham
  • 2,870
  • 5
  • 19
  • 33
0

Python 3.7 this will do what you want.

userName = input('Name: ')
age = int(input('age: '))
factor = 2
finalAge = age + factor
print("In", + factor, "years you will be", + finalAge, "years old " + userName + "!")
  • Might want to also point out that I am very new to Python. I would go ahead and throw some error handling in there to catch valueEror and typeError. If not you will find people entering strings into you int inputs and breaking your code. – Chris Stephens Sep 08 '18 at 00:47
0

I had the same problem with django and I solved it by clearing the chrome cache

-1

I think this type of error: TypeError: can only concatenate str (not "float") to str

The reason was Python use comma (,) to stick all element in print() function together. little different from Java that they used plus (+) sign to stick and output the result.

Note: double check all print() function to see did you put + sign and it should be replace by ,

Khai Lim
  • 41
  • 2