Str(num1 + num 2)? | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
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)?

2nd Jun 2019, 4:44 PM
Mohammad Akradi
Mohammad Akradi - avatar
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ā€™))
2nd Jun 2019, 4:52 PM
Lothar
Lothar - avatar
+ 1
Yes , str() constructs a string from a wide variety of data types, including strings, integer literals and float literals
2nd Jun 2019, 4:55 PM
**šŸ‡¦šŸ‡Ŗ|šŸ‡¦šŸ‡Ŗ**
**šŸ‡¦šŸ‡Ŗ|šŸ‡¦šŸ‡Ŗ** - avatar