liveBook · Manning

Split

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

str.split() is a Python string method that divides a string into a list of substrings using a separator or, by default, whitespace.

String splitting uses Python's str.split() method to divide text into a list of substrings. With no argument, it splits on whitespace; with an argument such as ',' it splits on that separator, which is omitted from the result. section 6.5 section 15.1.1

Use splitting to:

  • Count words approximately with len(line.split()), while recognizing that punctuation remains attached to neighboring text. section 6.1.3
  • Create lists economically from space-separated or multiline data, then verify their contents with len() and assertions. section 9.1.3
  • Parse simple comma-delimited records before pairing fields with headers using zip(); use a CSV-specific approach when the format requires more. section 19.1.2
  • Choose regular-expression splitting when token boundaries require flexible patterns or preservation of non-word text. section 15.1.1 section 16.1.5

Build a word counter

For each input line, count the resulting pieces: words += len(line.split()). This handles whitespace conveniently, but "Hello, world!" becomes ['Hello,', 'world!'], so it counts tokens rather than punctuation-free words. section 6.3.2

Parse a simple record

headers = 'exercise,reps'.split(',') values = 'Burpees,20-50'.split(',') record = dict(zip(headers, values)) # {'exercise': 'Burpees', 'reps': '20-50'} section 19.1.2

>>> adjectives = """
... bankrupt base caterwauling corrupt cullionly detestable dishonest
... false filthsome filthy foolish foul gross heedless indistinguishable
... infected insatiate irksome lascivious lecherous loathsome lubbery old
... peevish rascaly rotten ruinous scurilous scurvy slanderous
... sodden-witted thin-faced toad-spotted unmannered vile wall-eyed
... """.strip().split()
>>> nouns = """
... Judas Satan ape ass barbermonger beggar block boy braggart butt
... carbuncle coward coxcomb cur dandy degenerate fiend fishmonger fool
... gull harpy jack jolthead knave liar lunatic maw milksop minion
... ratcatcher recreant rogue scold slave swine traitor varlet villain worm
... """.strip().split()
>>> len(adjectives)
36
>>> len(nouns)
39

Inspect a multiline string split into adjective and noun lists, including whitespace cleanup and length checks. section 9.3.6

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

How deep it goes
Explains default whitespace and explicit separators through runnable examples, but defers robust word tokenization and full CSV handling.
Examples
Demonstrates word counting, multiline adjective lists, insult generation, text tokenization, gematria encoding, and manual CSV records.
What it assumes
Assumes familiarity with strings, lists, loops, and len(), while demonstrating splitting from small interactive examples.
What you can do after
Apply it via Python string methods to turn whitespace- or delimiter-separated text into lists for counting, validation, and further processing.
How it connects
Connects string splitting with lists, len(), join(), regular expressions, tokenization, file iteration, and zip().

Also explained in

Compare how 11 books explain split →

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.