Artificial Intelligence (AI) & Machine Learning Course for ManagersEnroll Now
Different types behave differently in Python. For example,
On integers it just sums up the integers
print(5 + 8)
It will print
While on strings it paste together(joins) the strings. To print the full name of person having first name as
John and last name as
print("John" + " " + "Barley")
" " here, it adds space while joining the strings. It will print
Now since we know the
+ operator we can print a message like this:
print("BMI is " + 25)
Run above code in the notebook. Did it run?
This will not work as you cannot simply sum strings and integers.
How do we fix it then?
To fix this error, you'll need to explicitly convert the
bmi, which is an integer, to a string. You'll need
str() to convert a value into a string.
str(bmi) will convert the integer
bmi to a string.
Pro Tip - Remember, we can only sum like types
print("BMI is " + bmi)on the notebook on the right hand side. Try to understand error message
bmito a string and assign it to
"BMI is "and
bmi_strand assign the result to
Taking you to the next exercise in seconds...