Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis0
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 Respostas
+ 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