0

Though it might seem a very trivial question, I still want to know the principle behind it. When we write multiple strings together without any comma,python concatenates them. I was under the impression that it will throw some error. Below is a sample output:

print('hello''world')
# This will output helloworld

Even if I write those multiple strings in the python REPL, the output will be the concatenated form of the strings. Can anyone please explain the logic behind this operation ?

khelwood
  • 55,782
  • 14
  • 81
  • 108
raniphore
  • 386
  • 1
  • 2
  • 9

1 Answers1

3

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

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.
Thus, "hello" 'world' is equivalent to "helloworld". 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

khelwood
  • 55,782
  • 14
  • 81
  • 108