@@ -133,13 +133,18 @@ Literals
133133
134134Python supports string and bytes literals and various numeric literals:
135135
136- .. productionlist :: python-grammar
137- literal: `stringliteral ` | `bytesliteral ` | `NUMBER `
136+ .. grammar-snippet ::
137+ :group: python-grammar
138+
139+ literal: `strings ` | `NUMBER `
138140
139141Evaluation of a literal yields an object of the given type (string, bytes,
140142integer, floating-point number, complex number) with the given value. The value
141143may be approximated in the case of floating-point and imaginary (complex)
142- literals. See section :ref: `literals ` for details.
144+ literals.
145+ See section :ref: `literals ` for details.
146+ Seee section :ref: `string-concatenation ` for details on ``strings ``.
147+
143148
144149.. index ::
145150 triple: immutable; data; type
@@ -152,6 +157,45 @@ occurrence) may obtain the same object or a different object with the same
152157value.
153158
154159
160+ .. _string-concatenation :
161+
162+ String literal concatenation
163+ ............................
164+
165+ Multiple adjacent string or bytes literals (delimited by whitespace), possibly
166+ using different quoting conventions, are allowed, and their meaning is the same
167+ as their concatenation. Thus, ``"hello" 'world' `` is equivalent to
168+ ``"helloworld" ``.
169+
170+ Formally:
171+
172+ .. grammar-snippet ::
173+ :group: python-grammar
174+
175+ strings: ( `STRING ` | `fstring ` | `tstring `)+
176+
177+ Note that this feature is defined at the syntactical level, so it only works
178+ with literals.
179+ To concatenate string expressions at run time, the '+' operator may be used::
180+
181+ greeting = "Hello"
182+ space = " "
183+ name = "Blaise"
184+ print(greeting + space + name) # not: print(greeting space name)
185+
186+ Also note that literal concatenation can freely mix raw strings,
187+ triple-quoted strings, and formatted or template string literals.
188+ However, bytes literals may not be combined with string literals of any kind.
189+
190+ This feature can be used to reduce the number of backslashes
191+ needed, to split long strings conveniently across long lines, or even to add
192+ comments to parts of strings, for example::
193+
194+ re.compile("[A-Za-z_]" # letter or underscore
195+ "[A-Za-z0-9_]*" # letter, digit or underscore
196+ )
197+
198+
155199.. _parenthesized :
156200
157201Parenthesized forms
0 commit comments