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?

1
  • '' means an empty string to which we join another string. It's a more pythonic way to do string concatenation Commented Feb 26, 2012 at 9:12

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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: stackoverflow.com/questions/476772/…
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.

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.