Why can I concatenate 2 strings but not a string and a floating value?

Viewed 171

This is some code I have been trying to do and as soon as I remove the " " from the character age the code doesn't work could someone please explain what I have done wrong and how I can fix it?

character_name = "Tom"
character_age = "50.86798"
is_male = False
print("There was once a man named " + character_name +  ",")
print("he was " + character_age + " years old")

character_name = "Mike"
print("He really liked the name " + character_name + ",")
print("but he didn't like being {0}.".format(character_age))
4 Answers

With the quotations, character_age is a string. Without the quotations, character_age is a float (floating-point number). When concatenating values (think: adding, combining, summing, etc.), a general rule of thumb is that only like values can be concatenated. Thus, when running this line:

print("he was " + character_age + " years old")

an exception will be raised if character_age is not a string.


If you want to run the code with character_age as a float, here are a few possible solutions (there may be, and probably are, more):

  1. Convert character_age to a string

This would involve calling the Python built-in str() function and passing character_age. This will return the float stored in character_age in its string representation:

character_age = 50.86798
...
print("he was " + str(character_age) + " years old")
  1. Use string formatting to format character_age into the string

Use whichever style of string formatting you like best, whether it be C-style string formatting, the str.fomat() method, or f-strings:

character_age = 50.86798
...
print("he was %s years old" % character_age)
print("he was {} years old".format(character_age))
print(f"he was {character_age} years old")

Basically when you are giving the double quotes the compiler is treating the variable character_age as string.

print("There was once a man named " + character_name +  ",")

So in the above line the plus(+) sign means to concatenate the different string values and show in as s new string without making any changes to the variable.

But while you are removing the double quotes, its treating the variable character_age as float. And additionally for the compiler, the meaning of plus(+) sign before a variable of type integer or float or double is to make addition arithmetic operation. But logically, we can't perform addition operation with one string and one float. That's why it's throwing error if you are removing the double quotes.

My suggested code will be,

character_name = "Tom"
character_age = 50.86798
is_male = False
print("There was once a man named " + character_name +  ",") # Here i have converted the veriable type float to string temporarily not permanent
print("he was " + str(character_age) + " years old")

This is a result of type mismatch when you're using addition operators. Over here:

print("he was " + character_age + " years old")

what you are doing when you remove the '"' is you're turning the character age into a float instead of a string, and you cannot add floats to strings without some conversion, like str().

This seems to be a very fundamental problem, so I highly recommend you learn Python a little more deeply to understand.

In the first code snippet, character_age is of float datatype. You cannot add a float to a string. That's like doing: "abc" + 12.34, which throws an error.

character_age = 50.86798
print(type(character_age))
print("HELLO " + character_age + " WORLD" )

But, if I added quotes to the same data, it becomes a string datatype. Python treats it as a str and not float. Hence, "abc" + "12.34" would result in "abc12.34", ie. concatenation.

character_age = "50.86798"
print(type(character_age))
print("HELLO " + character_age + " WORLD" )
Related