5

Why on Earth doesn't the interpreter raise SyntaxError everytime I do this:

my_abc = ['a',
          'b',
          'c'
          'd',]

I just wanted to add 'c' to the list of strings, and forgot to append the comma. I would expect this to cause some kind of error, as it's cleary incorrect.

Instead, what I got:

>>> my_abc
 ['a', 'b', 'cd']

And this is never what I want.

Why is it automatically concatenated? I can hardly count how many times I got bitten by this behavior. Is there anything I can do with it?

Just to clarify*: I don't actually mind auto-concatenation, my problem has to do ONLY with lists of strings, because they often do much more than just carry text, they're used to control flow, to pass field names and many other things.

5 Answers 5

13

Is called "Implicit String Concatenation" and a PEP that proposed its removal was rejected: http://www.python.org/dev/peps/pep-3126/

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

Comments

6

It's by design. It allows, for example, writing long string literals in several lines without using +.

3 Comments

I'd rather use """ for multilne strings or break them explicitly with a ``, than than this kind of magic concatenation.
""" puts newlines between lines.
It's not "magic". It's well-documented: docs.python.org/reference/…
3

As others said, it's by design.

Why is it so ? Mostly for historical reasons : C also does it.

In some cases it's handy because it reduce syntaxic noise and avoid adding unwanted spaces (inline SQL queries, complexes regexpes, etc).

What you can do about it ? Not much, but if it really happens often for you, try one of the following tricks.

  • indent your list with coma at the beginning of the line. It's weird, but if you do so the missing comas become obvious.
  • assign strings to variables and use variables list whenever you can (and it's often a good idea for other reasons, like avoiding duplicate strings).
  • split your list: for list of words you can put the whole list inside only one string and split it like below. For more than 5 elements it's also shorter.

    'a b c d e'.split(' ').

2 Comments

Or put a comma AFTER each item (including the last one); the interpreter allows the extra comma. That way if you format your list as one value per line and move items up or down you won't run into this issue.
That seems to be what did the original question poster (and that's also what I usually do for the reason you just said), but a missing coma at end of line can easily go unseen. The opposite problem also occured to me : a trailing coma at end of line changing a constant into tuple. I believe this one is even harder to detect than the other :-(
2

Because two string literals side-by-side, delimited by whitespace, are concatenated. Since the strings are within a list, they are 'side-by-side'.

See: http://docs.python.org/reference/lexical_analysis.html#string-literal-concatenation

2 Comments

The question is why. You've simply restated what is occuring, which the OP already has in his post. If a question asks "Why does 10*2 = 20", the answer "because 10*2 = 20" is not helpful.
The why is explained in the docs - it is by design and has use cases.
2

Because often people want to do something like this:

line = ("Here's a very long line, with no line breaks,"
        " which should be displayed to the user (perhaps"
        " as an error message or question box).")

It's easier to write this without having to manually concatenate strings. C, C++, and (I believe) Java and C# also have this behavior.

4 Comments

""" does not do this. It inserts newlines.
@kurczak: 40 years of developers disagree with you.
@John Millikin please elaborate
Automatic string concatenation has been present in most languages since at least C, which was released roughly 40 years ago. While age of a feature doesn't necessarily mean it's a good idea, the fact that it's 1) survived unchanged in dozens of descendant languages 2) been adopted by unrelated languages indicates that it's generally regarded as a good idea.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.