0%

🪄 Splitting Whispers Into Words

Divides text by spaces into a list of words. The original string always stays intact.
🔮 “hi again”.split()

A sentence is a stream of words connected by spaces. Sometimes you need to separate them. To break the stream into pieces. To hold each word alone in your hand. The split() method is the blade that cuts at every space. It transforms a single string into a list of smaller strings. Each piece is a word. Each word stands alone.

🕯️ Magic Note

When called with no arguments, split() divides the string at any whitespace: spaces, tabs, newlines. It then returns a list of substrings. Multiple consecutive whitespace characters are treated as a single separator, and empty strings are never returned.

The syntax “hi again”.split() scans the string, finds the space between “hi” and “again”, and splits there. The result is a list with two elements: [“hi”, “again”]. The original string stays unchanged because strings are immutable.
  • Without arguments splits on any whitespace: spaces, tabs, newlines
  • With a custom separator, pass it as an argument like .split(“,”)
  • Returns a list of strings
  • Empty strings produce [“”], not an empty list
💡 Use .split() without arguments to cleanly split words ignoring extra spaces.
Use .split(“,”) for CSV data or custom delimiters.
Use .split(maxsplit=1) to split only the first occurrence.
For splitting lines of text, consider .splitlines() which handles different newline characters more gracefully.
Input Stringsplit() CallOutput
“hi again”.split()[“hi”, “again”]
“one two three”.split()[“one”, “two”, “three”]
“a,b,c,d”.split(“,”)[“a”, “b”, “c”, “d”]
“hello world”.split()[“hello”, “world”]
“apple-banana-cherry”.split(“-“)[“apple”, “banana”, “cherry”]
⚠️ .split() without arguments treats multiple spaces as one separator and ignores leading or trailing spaces. But .split(” “) with a space argument behaves differently. It splits on every single space and can produce empty strings. Most of the time, the default behavior is what you want. For preserving empty fields in CSV data, use the csv module instead.
Examples

Python

# Basic word splitting

sentence = “code coffee conjure”

words = sentence.split()

print(words)

# Output: [“code”, “coffee”, “conjure”]

Python

# Using a custom separator

data = “name:age:city”

parts = data.split(“:”)

print(parts)

# Output: [“name”, “age”, “city”]

Python

# Limiting number of splits

text = “one two three four”

first_only = text.split(maxsplit=1)

print(first_only)

# Output: [“one”, “two three four”]

Common Mistakes
  • Confusing .split() (splits on any whitespace) with .split(” “) (splits on spaces only)
  • Expecting .split() to modify the original string, it returns a new list and leaves the string unchanged
  • Using .split() on extremely large strings without considering memory usage, the resulting list can be very large

⚡ Whisper

A stream of words flows together. The split cuts gently at every space. Each piece is freed. Each whisper becomes a word standing alone. Together they were one. Apart they are many.