Plz giv me a a little more simplified answer for '5' + 2 (why error) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Plz giv me a a little more simplified answer for '5' + 2 (why error)

12th Jan 2017, 12:04 PM
Ashley Mason
Ashley Mason - avatar
8 Answers
+ 11
I'm not a python developer, but I think what's happening is that '5' is a string and 2 is an int. It can't decide what to do with them, concatenate (52) or mathematically add them (7). You need to convert '5' to an int or 2 to string, depending what you want as a result. For result of (52) convert 2 to string, for result (7) convert '5' to int. http://www.pythonforbeginners.com/concatenation/string-concatenation-and-formatting-in-python Hope I helped! :)
12th Jan 2017, 12:32 PM
Йордан Петров
Йордан Петров - avatar
+ 3
Your '5' is a string and 2 is an int. You cannot sum them :-)
14th Jan 2017, 1:36 PM
Diego
Diego - avatar
+ 2
When you put brackets "5" or '5' Python thinks that this is a word not a number
12th Jan 2017, 12:27 PM
Abbos Zayniddinov
Abbos Zayniddinov - avatar
+ 1
got an idea.. thank u
12th Jan 2017, 12:26 PM
Ashley Mason
Ashley Mason - avatar
0
because '5' is not integer. int + string = error. Python dosent work like php
15th Jan 2017, 3:12 AM
Gor Julhakyan
Gor Julhakyan - avatar
0
convert str '5' to int, or int 5 to str.
15th Jan 2017, 3:15 AM
Gor Julhakyan
Gor Julhakyan - avatar
0
Yaa '5' this is assumed as string and 2 is an integer number... So not performes the mathematical operation
23rd Jan 2017, 4:00 PM
Cobra
Cobra - avatar
0
an error arouses whenever You try to concatenate a string with an integer or simply two variables not of the same type here Python has a tendency to take everything as a string present in between the " " or ' ' quotes In this case it takes '5' as a string while 2 is an integer to solve this problem write instead >>>print ('5' + str(2)) This would serve your purpose
4th Feb 2017, 12:18 PM
Shivam Bhatt