home ~ projects ~ socials

Remove Leading And Trailing Whitespace From A Python String With .strip()

original = """

    alfa bravo charlie

   """

stripped = original.strip()

print(f"START|{stripped}|END")
Output:
START|alfa bravo charlie|END

Notes

  • Calling .strip() removes both leading and trailing whitespace (including newline characters) from a string
  • This is called trim in some other languages
-- end of line --