strip Trims Both Ends, Keeps The Middle
strip, lstrip, and rstrip trim whitespace or any characters in a set from the ends; set-based stripping is not prefix removal, so use removeprefix for affixes.
Code
raw = " \t deploy notes \n "
# strip removes leading and trailing whitespace; the middle is untouched
print("strip:", repr(raw.strip()))
print("lstrip:", repr(raw.lstrip()))
print("rstrip:", repr(raw.rstrip()))
# An argument strips any of those characters, not a prefix or suffix
print("chars:", repr("7.50USD".strip("USD")))
print("chars:", repr("www.example.com".strip("w.moc")))
# Digit sets strip padding: 007 is not a prefix, it is just in the set
print("digits:", repr("007logs".strip("0123456789")))
# For real prefixes and suffixes use removeprefix/removesuffix instead
print("prefix:", "unpaid_2024".removeprefix("unpaid_"))
print("suffix:", "report_2024.csv".removesuffix(".csv"))Output
Captured from a sandboxed run of this snippet in the build pipeline.
strip: 'deploy notes'
lstrip: 'deploy notes '
rstrip: ' \t deploy notes'
chars: '7.50'
chars: 'example'
digits: 'logs'
prefix: 2024
suffix: report_2024Inside the snippet
Demo state sits in top-level bindings: raw.
What to watch for
- The runtime alone is required here — no packages, no install step.
Putting it to use
Look for the strip:, lstrip:, rstrip: prefixes once the file starts running; each one ties a printed line to the step that produced it.
Interactive Tools for This Topic
FAQ
- Can I paste this into a project as-is?
- Yes. Paste it in and run; no package manager step is needed. Its state lives in module-level bindings: `raw`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.