man_of_molybdenum man_of_molybdenum - 1 year ago 96
Python Question

Why exactly does the str.join() function get rid of quotation marks and brackets?

Why does the

function get rid of quotations and brackets in lists? For instance, if you have list
x = ['a', 'b', 'c', 'd']
print x
you'll get an output like this:
['a', 'b', 'c', 'd']
, but if you do
print " ".join(x)
you'll get a much nicer looking
a b c d
. How does simply adding a space inside a string get rid of both the brackets and quotation marks?

EDIT: Okay, I'm sorry I'm an idiot. I just didn't understand.

Answer Source

You're confused. The .join() statement turns the list (of strings) into a string and adds whatever is between the quotes between each element, so if you do

"hey".join(['0', '9'])

you'll get '0hey9'. You can think of it as taking every element in the list and connecting (or joining) it with the string which join() is being called on. So the reason there are no more brackets or quotation marks is because it is not a list anymore, it's a string.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download