Exercise 5: Difference between summing up and concenating variables

(Please excuse my english skills.)

Hi All! I was working through exercise 5, checked the part ‘What you should see’ and found a difference. The result of the addition, represented by the ‘total’ variable, was quite to high.

If I add 26, 189, and 82.3 I get 2618982.3.

After broking the code I realized it was not summing it up, the variables were just concatenated. I found this in the documentation:

Strings can be concatenated (glued together) with the + operator, and repeated with *

So now I’m confused: The code is doing what it’s opposed to do, but that’s not “What I should see”.

Could you please explain it to me?

My Code:

my_age = ‘26’
my_height = ‘189’ #cm
my_weight = ‘82.3’ #kg

total = my_age + my_height + my_weight
print(f"If I add {my_age}, {my_height}, and {my_weight} I get {total}.")

I also tried the code of ex5 with an * between the variables:

total = my_eyes * my_height * my_weight

Because in ex4 there was also nothing repeated when the code was:

carpool_capacity = cars_driven*space_in_a_car

but I got this error:

Traceback (most recent call last):
File “ex5.py”, line 15, in <module>
total = my_age * my_height * my_weight
TypeError: can’t multiply sequence by non-int of type ‘str’

Could you please give me hint, why the code is here acting different than it was in ex4?

(I understand much more than I can express. So, don’t feel needed to simplify.)


Hey @cheyder I will try to explain to you: In Python, and most programming languages, you have different types of variables, called data types. Python has five of it: Strings, Numbers, List, Tuple and Dictionary. Forget about the last three for a moment, you will learn about them later. As the types allready suggests in their names the string type is used to store text (strings) and the number type is used to store numbers.
When you want to store a number, you write just a number behind the equal sign of the variable, like this:

age = 12 # number

If you want to store a string, you write text behind the equal sign of the variable and sourround it with single or double quotes" (this is very important). So Python knows “Hey, this is a string”. It looks like this:

brother = "Max" # string with double quotes
sister = 'Anna' # string with single quotes

Attention: In Python you can also define numbers as a string! If you surround numbers like 1, 2, 3 with quotes, Python assumes it is a string not a number.
So if you write:

number_of_cars = "12" # this is not a number, it is a string

Python thinks this is a string, not a number and treats it like that.
So, in your case, you concatenate strings. You don’t add numbers, because you put a quote around the numbers.
Hope that helps.


Nice explanation @DidierCH

I sometimes forget how the use of “some string” is native to English language for speech and must seem like even more confusing syntax if it’s not your first language.

Thanks a lot!

I understood immediately. That’s probably the reason why the texteditor is showing the different data types in different colors, right? Oh, it can be so easy.

Your explaination really helped! Now I can go on, looking for the next obstacle…

@gpkesley, thanks for the additional hint! I thought of “string” as asomething like a “cord/line/twine” and didn’t really get a grasp. But I found a quite similar word in german “Zeichenkette” (string of characters) and it kind of clicked. So, I learned something about python and english vocabulary! Thanks for that, until next time!


Hallo @cheyder. Genau, Zeichenkette ist das Wort für string in Deutsch (kommt so in deutschen Programmierbüchern vor).

Glad I could help!