+ 13

How to concatenate string to an integer in python???

I was making a program in python for finding a square of a number, so I write this: print("Square of " + num + " is " + num**2) but this is giving an error. Please help me with it.

31st May 2020, 2:04 PM
ツSampriya😘ツ
ツSampriya😘ツ - avatar
13 RĂ©ponses
+ 6
I prefere the f- string mentioned by HonFu, but also with a simple print statement and without any conversion you can do that: num = 7 print("Square of" ,num, "is", num**2) So instead of using "+" as an operator for concatenation use "comma" to "merge" string and numerical information. This will be done implicitly by print function.
31st May 2020, 2:33 PM
Lothar
Lothar - avatar
+ 5
Lothar I your answer is helpful Thanks Bro
31st May 2020, 2:34 PM
ツSampriya😘ツ
ツSampriya😘ツ - avatar
+ 4
Thanks bro for helping me
31st May 2020, 2:16 PM
ツSampriya😘ツ
ツSampriya😘ツ - avatar
+ 4
HonFu I am a beginner to python so i am not understanding what you have tell. But thanks I will find it helpful some day. Thanks
31st May 2020, 2:35 PM
ツSampriya😘ツ
ツSampriya😘ツ - avatar
+ 3
You can also use a format string: print(f'Square of {num} is {num**2}')
31st May 2020, 2:23 PM
HonFu
HonFu - avatar
+ 2
and at second place where num**2 is written
31st May 2020, 2:07 PM
ツSampriya😘ツ
ツSampriya😘ツ - avatar
+ 2
#In simple way: print("Square of",num,"is",num**2) # str(): print("Square of " + str(num) + " is " + str(num**2)) # format1: print(f"Square of {num} is {num**2}") # format2: print("Square of {} is {}".format(num, num**2)) # format3: print("Square of %i is %i" %(num, num**2))
1st Jun 2020, 10:30 AM
Jenson Y
+ 1
Use format Or f string
1st Jun 2020, 12:58 PM
Yuvraj Chodha
Yuvraj Chodha - avatar
0
Just use str(variable)
1st Jun 2020, 2:45 AM
Sagun Karki
Sagun Karki - avatar
0
Solution Of Your Question Is : print(“Square Of”,num,”is”,num**2)
2nd Jun 2020, 9:25 AM
Rahul Saxena
Rahul Saxena - avatar
0
print("Square of" +str(num)+"is"+str(num**2))
2nd Jun 2020, 1:31 PM
Purushoth
Purushoth - avatar
0
print("Square of %d is %d" %(num,num**2))
2nd Jun 2020, 1:33 PM
Purushoth
Purushoth - avatar
- 1
Better Use f String Method This Will Help You Alot. x=2 square=pow(x,2) print(f” Square Of {x} Is : {square}”)
2nd Jun 2020, 9:15 AM
Rahul Saxena
Rahul Saxena - avatar