A simple question about string add operation | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

A simple question about string add operation

print(int("0010"+"1011")) #why output 00101011 is not correct?

22nd Feb 2018, 2:58 AM
呂羿賢
呂羿賢 - avatar
3 Answers
+ 2
thanks all~~~You all are so friendly .ok,I understand it.haha the question seem to be too stupid.
22nd Feb 2018, 3:13 AM
呂羿賢
呂羿賢 - avatar
+ 1
because when you use int() python by default eliminates all the 0's that are in front of the integer, if you want the 0's to show then don't use int ()
22nd Feb 2018, 3:05 AM
Joseph David Zamora Murillo
Joseph David Zamora Murillo - avatar
+ 1
Because you turned the value into an integer after adding the strings together, it discards the extra 0's and makes it 101011. Take off the int() in order to make it 00101011.
22nd Feb 2018, 3:07 AM
Colin
Colin - avatar