0
print('Hello''World')
>>> HelloWorld

How come this works, when multiple string arguments are not separated by comma? I found this unusual because, like any other function multiple parameters must be separated with commas. There should have been a syntax error.

0x5453
  • 12,753
  • 1
  • 32
  • 61
  • 1
    There are more than two ways of writing string literals in your code: `'Hello'` and `"Hello"` are two examples. Since you can use the `'` character as a delimiter, then you can have: `'Hello"World'`. Except your formatting was off in your question and I mistook your `''` for `"`. – quamrana Jan 14 '21 at 15:04
  • Python concatenates strings directly written behind each other. Just try `'Hello''World'` in the interactive interpreter. – Klaus D. Jan 14 '21 at 15:05
  • [*"Multiple adjacent string or bytes literals (delimited by whitespace), possibly using different quoting conventions, are allowed, and their meaning is the same as their concatenation."*](https://docs.python.org/3/reference/lexical_analysis.html#string-literal-concatenation) – Olvin Roght Jan 14 '21 at 15:06
  • Many languages concatenate string literals in this way. It's useful when you want a very long string but don't want your lines to be very long. – molbdnilo Jan 14 '21 at 15:11

1 Answers1

1

In Python, adjacent string literals are concatenated by default.

s = 'Hello'  "world"    '''!'''      """?"""

This is perfectly fine. I used different quotes for each literal, and even separate them with spaces, and everything is still fine.

kwkt
  • 1,058
  • 3
  • 10
  • 19
  • Thanks, for answering...Can I surmise that, since adjacent string literals are concatenated by default, it is effectively a single parameter, hence absence of a comma doesn't produce an error. – Parthasarathi Banerjee Jan 15 '21 at 04:13
  • @ParthasarathiBanerjee You are absolutely correct. If curious, you can consult the link on the comment above for more information. – kwkt Jan 15 '21 at 07:02