-3

I'm new to Python and I know you use "+" to concatenate strings like the following:

'foo' + 'bar'

Today I came across some code where it seems to concatenate strings using spaces(indentations) [Update: wrong example, see Edit 2 below]:

SQL = "select col1, col2 "
      "from table"

I didn't find this feature got mentioned in the book I'm reading or anywhere on the web. Can someone please explain to me that:

  1. What does the space mean in this context? Why does it work?
  2. When would you prefer spaces over "+"? For readability when concatenating strings span multiple lines?

Edit: Thank you for all your responses. After trying the code out myself it indeed didn't work. I probably missed some details when looking at the code before I left for work; so I'll check it out tomorrow. However, the following did work (and this made me assume the SQL example works without verifying first):

x = "foo" "bar"    # "foobar"

Edit 2: It turned out that I missed a pair of parentheses in the SQL example above. The actual code looks like this:

SQL = ("select col1, col2 "
       "from table")
xli
  • 2,420
  • 2
  • 20
  • 27
  • 2
    Re: 1. It doesn't work. :) – Jacob Krall Apr 11 '17 at 01:41
  • 3
    Are you sure that was the exact code? Have a look at this, it might help: http://stackoverflow.com/questions/10660435/pythonic-way-to-create-a-long-multi-line-string – Rhexis Apr 11 '17 at 01:43
  • 1
    See also http://stackoverflow.com/questions/18842779/string-concatenation-without-operator-in-python, which has a link to the docs regarding the rules around adjacent string literals. – Jacob Krall Apr 11 '17 at 01:45
  • 1
    The code would have to be `SQL = ("select col1, col2 "` on one line and `"from table")` on the next for that to work. This is because in general, two strings next to one another are automatically concatenated. The parenthesis around the expression defining `SQL` allow it to be split onto more than one line of code (like auto-continuation). – martineau Apr 11 '17 at 01:52

2 Answers2

2

String concatenation works automatically in python so there is no need for the + in your first example. You can do:

>>> "foo" "bar"
'foobar'

But if you want a space between foo and bar, either needs to be added to the end of one string or the beginning of the next.

>>> "foo " "bar"
'foo bar'

Parenthised expressions can extend to multiple lines. Once you open a paren, python knows to ignore newlines until you close it. So this also works

>>> ("foo"
... "bar")
'foobar'

>>> ("foo "
... "bar")
'foo bar'

And expressions in function calls can also have newlines

>>> def baz(sql=''):
...     print(sql)
... 
>>> baz(sql="foo "
...     "bar")
foo bar
tdelaney
  • 73,364
  • 6
  • 83
  • 116
2

From the String literals.

String literals can be enclosed in matching single quotes ' or double quotes ". They can also be enclosed in matching groups of three single or double quotes (these are generally referred to as triple-quoted strings). The backslash \ character is used to escape characters that otherwise have a special meaning, such as newline, backslash itself, or the quote character.

SQL = "select col1, col2 "
      "from table"

The above code is invalid; you need a backslash character:

SQL = "select col1, col2 "\
      "from table"

String literals can span multiple lines. One way is using triple-quoted strings:

s = """select col1, col2
      from table"""

But they're different, triple-quoted strings maybe give you extra whitespace, tabs and newlines.

zondo
  • 19,901
  • 8
  • 44
  • 83
McGrady
  • 10,869
  • 13
  • 47
  • 69