0
Str(num1 + num 2)?
If we don't use str command and add two numbers as float, can we show that with print("result is: ", num1+num2)?
2 RĂ©ponses
+ 5
Hope i understand what you mean:
num1 = 4.5
num2 = 7.2
print('result is: ', num1 + num2)
#output is: result is: 11.7
if you get the values for num1 and num2 from input() function numbers has to be converted to int or float. The reason is that input() always returns a string like â4.5â and â7.2â. Addition can not be done with strings, the strings will be concatenated:
num1 = input(ânum1â)
num2 = input(ânum2â)
#num1 shows â4.5â
#num2 shows â7.2â
print('result is: ', num1 + â â+ num2)
#output is: result is: â4.2 7.2â
to get int or float change input() to:
num1 = float(input(ânum1â))
+ 1
Yes , str() constructs a string from a wide variety of data types, including strings, integer literals and float literals