>>> a = [1,2]
>>> a = a + "ali"
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can only concatenate list (not "str") to list
For list data type use the .append
method:
>>> a = [1,2]
>>> a.append("ali")
>>> a
[1, 2, 'ali']
The String in Python is defined as a series of characters in a row. So, these "characters" are added to the "list" type as the characters, not as "String". When you want use the +=
adding operator for the list type, you must specify the type of the added variable or value using square brackets:
>>> a = [1,2]
>>> a += ["ali"]
>>> a
[1, 2, 'ali']
>>> a += ["foo",3,"bar"]
>>> a
[1, 2, 'ali', 'foo', 3, 'bar']