liveBook · Manning

Split

As explained in Get Programming: Learn to code with Python

split is a Python string method that divides a string at occurrences of a separator and returns the resulting parts as a list.

split is a Python string method that divides one string wherever a chosen separator appears and returns the resulting pieces as a list. This turns long or awkward text into smaller parts that are easier to inspect and process. section 8.0 section 26.3

Separate email addresses

Given emails = "zebra@zoo.com,red@colors.com,tom.sawyer@book.com,pea@veg.com", write emails_list = emails.split(','). The result is ['zebra@zoo.com', 'red@colors.com', 'tom.sawyer@book.com', 'pea@veg.com']. Each address becomes a separate list element. section 26.3

Use split when a repeated character separates the pieces you need:

  • Split comma-separated city names, then sort the resulting list. section 26.5
  • Split a cleaned document string on spaces to produce a list of words. section 29.3
  • Try quick checks such as splitting alphabet characters by a space, words by spaces, or a sentence on the letter s. section 26.3

Prepare document text

A word-extraction function first replaces newlines with spaces and removes punctuation, then applies text.split(" ") and returns the resulting list. Testing it on a short file first makes errors easier to find before processing a large document. section 29.3

emails_list = emails.split(',')

This short listing shows the central operation: calling split(',') on a string and storing the resulting list. section 26.3

How Get Programming: Learn to code with Python covers it

How deep it goes
Demonstrates one-line conversion, explains the resulting list, and applies splitting after text cleanup.
Examples
Uses comma-separated emails and cities, cleaned document text, and short separator-focused practice questions.
What it assumes
Uses strings and lists as the working objects, and contrasts splitting with manual index searches and repeated variables.
What you can do after
Apply it by converting comma- or space-delimited strings into lists for sorting and word processing.
How it connects
Connects string splitting to lists, string manipulation, text cleanup, sorting, and document similarity.

Also explained in

Compare how 11 books explain split →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Get Programming: Learn to code with Python. The section links point to the parts of the book it draws on.