Technical skill practice

split in Python: Whitespace, Delimiters and maxsplit

Python str.split returns a list of text pieces. Calling split without a separator groups whitespace and discards empty edge pieces. An explicit separator follows different rules: repeated delimiters can produce empty fields. maxsplit limits the number of splits, leaving the rest together.

Start with a small, explicit task

Parse two synthetic input rows: one is loosely spaced text, and one uses commas with intentionally empty columns. Preserve the comma row’s missing fields rather than cleaning them away accidentally. Then split a label from a free-text message that itself contains more colons.

Run the complete example

This original example was checked with Python 3.12.14. Save it as python_split.py and run it with Python 3.12. It uses only the standard library. The assertions check the stated results.

Expected output

['alpha', 'beta', 'gamma']

['alpha', '', 'gamma', '']

['note', ' keep: this part']

spaced = "  alpha   beta\tgamma  "
delimited = "alpha,,gamma,"
words = spaced.split()
fields = delimited.split(",")
message = "note: keep: this part".split(":", maxsplit=1)
assert words == ["alpha", "beta", "gamma"]
assert fields == ["alpha", "", "gamma", ""]
assert message == ["note", " keep: this part"]
assert "".split() == [] and "".split(",") == [""]
print(words)
print(fields)
print(message)

Explain the result

The whitespace example treats spaces and the tab as separators, yielding three words. The explicit comma example retains the empty field between two commas and the final empty field after the last comma. Those empty values may represent meaningful missing columns; filtering them out would change the record layout.

maxsplit=1 creates only two pieces in the message. The second colon stays inside the remainder, and the leading space after the first colon is preserved. Splitting and trimming are separate operations. The empty-string assertions show another important difference: default splitting has no words, while an explicit delimiter still produces one empty field.

Avoid these mistakes

split(" ") is not the same as split(): it targets a single space separator and can retain empties from repeated spaces. Plain split also does not understand quoted CSV fields containing commas. Use a real format parser when the input’s quoting or escaping rules require one.

Questions, answered

How do I keep the rest of a message together?

Use maxsplit=1 when only the first separator should divide the string.

Why are empty strings in the result?

With an explicit separator, repeated or trailing delimiters can represent empty fields, as this comma row demonstrates.

Sources and examples

ResumizeAI team. Examples are fictional teaching material, not customer results. Replace sample details with your own facts.

Your experience. A complete resume.

Build in Resumize with ATS-friendly templates, guided editing and a live preview. Start free, then upgrade when you need more.

Free account required. Free-plan PDF exports include a watermark.