Python’s string method split() divides a string into a list of substrings. With no argument, it splits on whitespace, including spaces, tabs, and newlines. Pass a separator to split on a particular sequence instead. The separator is not included in the returned substrings. section 6.4.1 section 8.13.2
Choose the boundary
"You\t can\n split this".split() returns ['You', 'can', 'split', 'this']. "Mississippi".split("ss") returns ['Mi', 'i', 'ippi']. For a delimited record, "Illinois|1979/01/01|17.48|994".split("|") returns four fields—all still strings. section 6.4.1 section 21.2.3
An optional second argument limits the number of splits. For example, 'a b c d'.split(' ', 1) returns ['a', 'b c d'], leaving the remaining text together. To set a limit while retaining whitespace splitting, supply None as the first argument. section 6.4.1
Common uses need different boundaries:
- Count whitespace-separated words in each file line with len(line.split()). section 8.13.2
- Split text on "\n\n" when blank lines separate paragraphs. section 21.2.2
- Split a known delimited record, then convert fields that need non-string types. section 21.2.3
Splitting is not text normalization: punctuation and case can still make otherwise matching words differ. Also, splitting a whole file on "\n" removes the newline characters; summing the lengths of the resulting pieces will undercount characters. For character counts, process the original lines instead. section 21.2.2 section 8.13.2










