home ~ socials ~ projects ~ rss

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

October 2023
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
Share link:
https://www.alanwsmith.com/en/2x/gk/id/xo/?remove-leading-and-trailing-whitespace-from-a-python-string-with-strip