+ 2

Why the append and concatenation (+) works differently for strings in list?

s=[1,2,3] s.append("text1") s+="text2" print(s) [1, 2, 3, 'text1', 't', 'e', 'x', 't', '2'] rather than [1,2,3,'text1,'text2']

25th May 2018, 12:27 AM
ranjith kumar
ranjith kumar - avatar
1 ответ
+ 1
For list, Python is actually doing same thing behind the hood (appending) When you do s += "text2" it's short form of for j in "text2": s.append(j) Thus, If s = [ ] s+="abcd" will give different result as s.append("abcd")
25th May 2018, 1:23 AM
E_E Mopho
E_E Mopho - avatar