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










