liveBook · Manning

Join

As explained in Tiny Python Projects: Learn coding and testing with puzzles and games

str.join() is a Python string method that concatenates strings from an iterable with the calling string inserted between adjacent elements.

Python's str.join() concatenates elements from an iterable, inserting the calling string between adjacent elements. It returns a new string and leaves the original list unchanged. section 3.3.9 section 3.8

Join picnic items

Given items = ['soda', 'sammiches', 'chips', 'apples'], write ', '.join(items) to produce 'soda, sammiches, chips, ice cream' in the demonstrated interaction. The separator belongs before .join(), which receives the list. section 3.3.9

Use join() when a program has assembled pieces that must become one string:

  • Combine randomly selected adjectives with ', '.join(random.sample(adjectives, k=3)), then insert the result into an f-string. section 9.3.6
  • Combine verse lines with '\n'.join(lines), or separate complete verses with '\n\n'.join(verses). section 13.3.1 section 13.3.3
  • Reconstruct processed text with ''.join(...), preserving the original characters between transformed words. section 16.3.1
  • Create a password by joining sampled words on the empty string. section 20.1.6
>>> ', '.join(items)
'soda, sammiches, chips, ice cream'

Inspect the interactive join example and its comma-space output. section 3.3.9

How Tiny Python Projects: Learn coding and testing with puzzles and games covers it

How deep it goes
Explains caller syntax, separators, returned strings, and several compositions, but does not descend into implementation internals.
Examples
Demonstrates comma-separated picnic items, generated insults, song verses, scrambled text, and sampled-word passwords.
What it assumes
Assumes familiarity with lists, iteration, slicing, and basic string operations while reinforcing list-to-string assembly.
What you can do after
Apply it via Python's str.join() to turn iterable elements into formatted strings with controlled separators.
How it connects
Connects string construction with lists, str.split(), random.sample(), f-strings, map(), regular expressions, and file output.

Also explained in

Compare how 6 books explain join →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Tiny Python Projects: Learn coding and testing with puzzles and games. The section links point to the parts of the book it draws on.