split()
A string method that cuts a string into a list of parts, at white space by default or at a separator you give.
In more detail
"a b c".split() gives ["a", "b", "c"], ignoring runs of spaces; "a,b,,c".split(",") gives ["a", "b", "", "c"], keeping the empty piece. For real comma-separated files use the csv module, which also handles quoted commas.
Example
"to be or not".split()Related terms
- join() — A string method that glues a list of strings together with the string it is called on between them.
- whitespace — Characters that show as blank space, such as spaces, tabs and line breaks.
- separator — The string placed between items when they are joined, or removed between parts when a string is split.
- csv module — The standard library module for reading and writing CSV files correctly, including quoted fields that contain commas.
Taught in
- The Print Function And String Literals — Hands-on Python: Complete Video Course & Book
- Split And Join — Hands-on Python: Complete Video Course & Book
- Chapter Two In One Sitting — Hands-on Python: Complete Video Course & Book