0

I just discovered what I thought was a typo but Python accepts it.

foo = {'a': 'b'}
foo.get('a'"")

returns 'b'. Where in Python is 'a'"" defined as a valid parameter to a function?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Brad
  • 735
  • 1
  • 8
  • 15
  • 1
    In [the language reference](https://docs.python.org/2/reference/lexical_analysis.html#string-literal-concatenation)? See also [an attempt to remove it](http://legacy.python.org/dev/peps/pep-3126/). – jonrsharpe Jun 01 '15 at 21:42

1 Answers1

2

Python concatenates all consecutive strings. It doesn't matter if that's in a function or elsewhere.

See the String literal concatenation section of the reference documentation:

Multiple adjacent string literals (delimited by whitespace), possibly using different quoting conventions, are allowed, and their meaning is the same as their concatenation.

This is done at the parsel level; the final bytecode stores just the one string object. See What is under the hood of x = 'y' 'z' in Python?

The feature was copied from C. From a (failed) proposal to remove the feature:

Many Python parsing rules are intentionally compatible with C. [...] In C, implicit concatenation is the only way to join strings without using a (run-time) function call to store into a variable.

The feature is very useful when producing long strings:

long_value = (
    'The quick brown fox jumped over the lazy fox and kept '
    'the string within the 80 character boundary.'
)
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343