1
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?

msw
  • 42,753
  • 9
  • 87
  • 112
Dewsworld
  • 13,367
  • 23
  • 68
  • 104
  • `''` means an empty string to which we join another string. It's a more pythonic way to do string concatenation – zengr Feb 26 '12 at 09:12

3 Answers3

6

'' 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.

zengr
  • 38,346
  • 37
  • 130
  • 192
  • Yes, if you see the benchmarks in the blog post, it faster than string+string concat. So, yes. Better performance and more pythonic. Similar question: http://stackoverflow.com/questions/476772/python-string-join-performance – zengr Feb 26 '12 at 09:38
3

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.

Anders Sjöqvist
  • 3,372
  • 4
  • 21
  • 22
0

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.

Pierce
  • 564
  • 2
  • 8