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


I am following a tutorial and I am getting an error.

My code should be this:

salaries = {'John':'20','Sally':'30','Sammy':'15'}

salaries['John'] = salaries['John'] + 30

I am getting back an error like this

Traceback (most recent call last): File “”, line 9, in

salaries[‘John’] = salaries[‘John’] + 30 TypeError: can only concatenate str (not “int”) to str

Can you help me with this?

Asked By: Alex



If you wanted to include the 30 you’d have to put something like str(30). That’s why it’s giving you that error cause 30 is an int and the rest are strings you can’t combine strings and ints. Hope this helps

Answered By: Alex Tănăsescu

This should fix it:

salaries['John'] = str(int(salaries['John']) + 30)

You need to convert the salaries of John to an int add 30 and then convert it back to a string.

This will change salaries['John'] from 20 to 50

Answered By: Tobias Wilfert

The “+” operator is using for concatenate strings, adding numbers, etc.
in your case you trying to add two integers but in your dictionary “salaries” the values are strings.
you can convert the value to int, adding the numbers and then convert to string to store the value.

Try this:

salaries['John'] = str(int(salaries['John']) + 30)
Answered By: lior
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.