+ 2

Python - Why result is adbdc and NOT adbdcd?

l = ['a','b','c'] print('d'.join(l))

10th Feb 2020, 6:34 PM
Paolo De Nictolis
Paolo De Nictolis - avatar
2 Answers
+ 7
concatenates the elements with 'd' (or puting 'd' between them) or joins elements of l with char 'd'. If it was adbdcd then it should be one more 'd' before a, thus 'd'adbdc'd'. It is how it works... I mean thats it. Also join is used for returning lists as strings. check also (join is already explained): https://www.sololearn.com/Discuss/1744468/how-to-use-join-work-JUMP_LINK__&&__python__&&__JUMP_LINK Now, why it is constructed like that? This is a hard question. It's just the language architecture... At the creation time, someone thought that such a method could be usefull and he just implemented it.
10th Feb 2020, 6:57 PM
Black Winter
+ 4
Because method join returns a string made from the elements of an iterable. The separator between elements is the string providing this method. '-'.join(['A', 'B', 'C']) # '-' string is the seprator 'A-B-C'
10th Feb 2020, 7:00 PM
Kosarevsky Dmitry
Kosarevsky Dmitry - avatar