Sponsored Reserved space — layout preview until AdSense is connected

Strip and normalize text

Remove stray whitespace and compare user input reliably.

7 lines 2 views 0 copies Updated May 23, 2026
Try in editor

How it works

User input often arrives with accidental spaces. strip() removes leading and trailing whitespace; lstrip() and rstrip() trim one side only.

For comparisons, normalize first — then lower() if case should not matter.

This pattern prevents silent bugs in forms, CLI tools, and config readers.

Python
Try in editor
messy = "  Hello, Python!  \n"
clean = messy.strip()
print(repr(clean))

user = "  YES  "
if user.strip().lower() == "yes":
    print("Confirmed")

Sponsored

Sponsored Reserved space — layout preview until AdSense is connected

Run this sample

Open the browser IDE to tweak the example and see results without installing anything.

Open editor

More from Strings & text