Note: This site is currently "Under construction". I'm migrating to a new version of my site building software. Lots of things are in a state of disrepair as a result (for example, footnote links aren't working). It's all part of the process of building in public. Most things should still be readable though.

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

Calling `.strip()`` on a string removes both leading and trailing whitespace (including newline characters)

Code

original = """

    alfa bravo charlie

   """

stripped = original.strip()

print(f"START|{stripped}|END")

Results

START|alfa bravo charlie|END