liveBook · Manning

Concatenation

As explained in Python How-To

String joining is the operation of combining multiple strings into one string with an optional separator.

String concatenation combines separate strings into one string. Use it when a small number of text fragments must become a readable result, such as a display label or a sentence. section 2.3 section 2.3.4

# initial input
fruit0 = "apple"
fruit1 = "banana"
fruit2 = "orange"

# desired output
liked_fruits = "apple, banana, orange"

Joining three fruit strings with comma-space separators produces one display string: "apple, banana, orange". section 2.3

Choose the joining technique according to the number and organization of the strings:

  • Use concatenation operators for a small number of strings. section 2.3.4
  • Prefer f-strings when related values and expressions need to be brought together clearly. section 2.3.4
  • Use join when individual strings are stored in an iterable or when many strings must be combined. section 2.3.4 section 2.6

Building a numbered label

for num_i in range(len(prime_numbers)): num_pos = num_i + 1 num = prime_numbers[num_i] print("Prime Number #" + str(num_pos) + ": " + str(num)) section 1.4.1

How Python How-To covers it

How deep it goes
Explains operator-based joining and contrasts it with f-strings and join, without descending into implementation internals.
Examples
Uses comma-separated fruit names and numbered prime output as concrete joining examples.
What it assumes
Assumes basic variables and strings while connecting joining decisions to iterables and formatted output.
What you can do after
Apply concatenation to construct readable output from a small number of string values.
How it connects
Connects concatenation with f-strings, join, split, string formatting, and iterables.

Also explained in

Compare how 10 books explain concatenation →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Python How-To. The section links point to the parts of the book it draws on.