1

I'm wondering why the following python array is valid (notice the missing comma between 'foo' and 'bar':

a = ['hello', 'world', 'foo' 'bar']

Printing 'a' outputs the following:

['hello', 'world', 'foobar']

Why is it even allowed, to omit a comma, if there is at least one comma? I thought there'd be an error like

You started the array with comma delimited strings, why on earth are you now, all of a sudden, omitting commas? Are you sure that's not mistake?

samwise
  • 1,907
  • 1
  • 21
  • 24
  • 2
    When you print `a` it should be `['hello', 'world', 'foobar']` not `['hello', 'worldfoobar']` – Abdul Niyas P M Dec 08 '17 at 09:49
  • 1
    I get `['hello', 'world', 'foobar']` (python 3) . What version of python are you using? – sheldonzy Dec 08 '17 at 09:49
  • 1
    This is by design, Python concatenates adjacent string literals. [*"This feature can be used to reduce the number of backslashes needed, to split long strings conveniently across long lines, or even to add comments to parts of strings..."*](https://docs.python.org/3/reference/lexical_analysis.html#string-literal-concatenation) See e.g. https://stackoverflow.com/questions/34540634/issue-warning-for-missing-comma-between-list-items-bug for other ways to check for this – jonrsharpe Dec 08 '17 at 09:50
  • I edited, of course it's ```['hello', 'world', 'foobar']``` – samwise Dec 08 '17 at 09:53

0 Answers0