@@ -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+ See section :ref: `string-concatenation ` for details on ``strings ``.
147+
143148
144149.. index ::
145150 triple: immutable; data; type
@@ -152,6 +157,58 @@ 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::
168+
169+ >>> "hello" 'world'
170+ "helloworld"
171+
172+ Formally:
173+
174+ .. grammar-snippet ::
175+ :group: python-grammar
176+
177+ strings: ( `STRING ` | fstring)+ | tstring+
178+
179+ This feature is defined at the syntactical level, so it only works with literals.
180+ To concatenate string expressions at run time, the '+' operator may be used::
181+
182+ >>> greeting = "Hello"
183+ >>> space = " "
184+ >>> name = "Blaise"
185+ >>> print(greeting + space + name) # not: print(greeting space name)
186+ Hello Blaise
187+
188+ Literal concatenation can freely mix raw strings, triple-quoted strings,
189+ and formatted string literals.
190+ For example::
191+
192+ >>> "Hello" r', ' f"{name}!"
193+ "Hello, Blaise!"
194+
195+ This feature can be used to reduce the number of backslashes
196+ needed, to split long strings conveniently across long lines, or even to add
197+ comments to parts of strings. For example::
198+
199+ re.compile("[A-Za-z_]" # letter or underscore
200+ "[A-Za-z0-9_]*" # letter, digit or underscore
201+ )
202+
203+ However, bytes literals may only be combined with other byte literals;
204+ not with string literals of any kind.
205+ Also, template string literals may only be combined with other template
206+ string literals::
207+
208+ >>> t"Hello" t"{name}!"
209+ Template(strings=('Hello', '!'), interpolations=(...))
210+
211+
155212.. _parenthesized :
156213
157214Parenthesized forms
0 commit comments