a = ('a', 'b', 'c')
print( ''.join(a) )
What is the meaning of ''
on the line two? Does string module and this ''
object has any difference?
a = ('a', 'b', 'c')
print( ''.join(a) )
What is the meaning of ''
on the line two? Does string module and this ''
object has any difference?
''
means an empty string to which we join another string. It's a more pythonic way to do string concatenation.
Check this out for more insights: http://www.skymind.com/~ocrow/python_string/
PEP3126 Says (although its rejected):
Instead of:
"abc" "def" == "abcdef"
authors will need to be explicit, and either add the strings:
"abc" + "def" == "abcdef"
or join them:
"".join(["abc", "def"]) == "abcdef"
So, both are same things, join is just more pythonic.
The code snippet you provided creates a string object containing an empty string, and then calls one of its methods. This is one of several ways to concatenate strings.
join is a metod for str. So, the '' in ''.join(iterable) is the separator between elements, and the elements are the items in the iterable (they need to be strings). It's very useful, particularly when the separator is not just '', but something like ','.join(iterable)---this gives you a single string that is all the items in iterable separated by commas.