Strings Not Getting Added Properly


#1

There is an example in the prompt here that I believe is incorrect:

number1 = “100”
number2 = “10”

string_addition = number1 + number2
#string_addition now has a value of “10010”

Why would string_addition not be 110?
This is also the result when you run the code


FAQ: Learn Python - Python Syntax - ValueError
#2

Hi @ls7891,

Adding two strings together concatenates them, even if the contents of the strings are numbers. So the behavior you’re observing is correct.

>>> "100" + "10"
'10010'

Will work the same as:

>>> "lorem" + " ipsum"
'lorem ipsum'

If you want to get the total of two numbers, you need to use integers for the addition, rather than strings:

>>> 100 + 10
110
>>> int("100") + int("10")
110