+ 2

Why can't I concatenate strings and integers?

How can i change this code so I get the needed output? Code : x = float(input("Enter a number - ")) print("\nThe square of the number you have entered is - " + x**2) Output : Enter a number - 14 The square of the number you have entered is - 196.0

2nd Dec 2016, 2:10 PM
!byteCode
!byteCode - avatar
4 ответов
+ 3
hi !byteCode, try str (x**2) hope this will help you its convert the integer to string
2nd Dec 2016, 2:19 PM
THDevelop
THDevelop - avatar
+ 3
If you don't want to convert the answer explicitly to string then, you can use (,) comma instead of (+) plus in between string and integer to get the desired output.
2nd Dec 2016, 2:29 PM
Aniruddh Dubey
Aniruddh Dubey - avatar
+ 2
print( "the square of no. you have entered "+str(x**2))
3rd Jan 2017, 5:37 AM
Sandeep Chatterjee
0
The input is a floating type. Then the output of the final answer is processed in the last statement. ie squaring . And the output is displayed.
3rd Jan 2017, 9:00 AM
Devaashish Sharma