+ 5

List += “abc” (python)

Hi everyone, Can anybody explain me why: List = [] List += “abc” Print (list) Output: [a,b,c] Why output come as separate letters, and not as whole string? Thanks,

18th Feb 2018, 10:15 PM
Denis
Denis - avatar
4 Answers
+ 8
To just add "abc" as is try : List.append("abc")
18th Feb 2018, 10:41 PM
Louis
Louis - avatar
+ 3
If you want a whole string, start with: List = “” instead of: List = [] Then you’ll be working with a string. You might want to reconsider the name of the variable then :P
19th Feb 2018, 1:39 AM
Pedro Demingos
Pedro Demingos - avatar
+ 1
Thanks guys
18th Feb 2018, 10:47 PM
Denis
Denis - avatar
- 1
In most languages, strings are just an array of chars all mushed together into a data type known as a string. Because you're adding the string to the list, it's basically the same as adding two lists together, resulting in all of the elements in the lists to all be put together into one whole list. Hope this helped!
18th Feb 2018, 10:24 PM
Faisal
Faisal - avatar