Skip to content

Computer Science · Ch 8 — Strings

Concatenation

8.3.1

Concatenation

To concatenate means to join. Python joins two strings end to end using the concatenation operator, written as the plus symbol +. When both operands are strings, + does not add numbers — it glues the second string onto the end of the first and returns the combined string.

>>> a = 'Concept'        # first string
>>> b = 'First!'         # second string
>>> a + b                # concatenated strings
'ConceptFirst!'

Two details deserve attention:

  • No space is inserted automatically. The result is exactly the characters of the first string followed immediately by the characters of the second. If a space is wanted, it must itself be part of one of the strings (e.g. a + ' ' + b).
  • The original strings are unchanged. Concatenation builds a brand-new string; the operands keep their old values — the immutability rule of Section 8.2.2 in action:
>>> a          # a is the same after the operation
'Concept'
>>> b          # so is b …