split() Collapses Whitespace And rsplit Limits Splits

No-argument split() treats any whitespace run as one separator and drops the edges, while rsplit with maxsplit keeps the left side intact for extension splits.

PythonCli Tooling

Code

pythonsnippet
18 lines
line = "  deploy  web.prod  --wait\n"

# split() with no arguments: any run of whitespace counts as one separator, and leading/trailing whitespace is dropped
print("no-arg:", line.split())

# explicit " " separator: consecutive spaces produce empty strings
print("space:", "a  b".split(" "))

# rsplit cuts from the right and maxsplit caps the count: the left side stays intact
spec = "app.web.prod.internal"
print("rsplit:", spec.rsplit(".", 1))
print("split:", spec.split(".", 1))

# typical use: split the extension off the right side of a filename
filename = "report-2026-11-21.final.pdf"
stem, ext = filename.rsplit(".", 1)
print("stem:", stem)
print("ext:", ext)

Output

Captured from a sandboxed run of this snippet in the build pipeline.

no-arg: ['deploy', 'web.prod', '--wait']
space: ['a', '', 'b']
rsplit: ['app.web.prod', 'internal']
split: ['app', 'web.prod.internal']
stem: report-2026-11-21.final
ext: pdf

The way the file runs

The file’s state is a handful of top-level bindings: line, spec, filename.

What to watch for

  • The runtime alone is required here — no packages, no install step.

Putting it to use

Each step the file takes is announced in the output: no-arg:, space:, rsplit:.

Interactive Tools for This Topic

FAQ

Does the runtime need any help from npm, pip, cargo, or the like?
No — the package manager has nothing to do here; nothing is declared. Its state lives in module-level bindings: `line`, `spec`, `filename`.