Remove Whitespace From The End Of Python Strings With Strip

August - 2021

Use .strip() to remove whitespce from the beginning and end of a string:

has_whitespace = '   giraffe  '
whitespace_removed = has_whitespace.strip()

Then:

print(f"---{has_whitespace}---")
print(f"---{whitespace_removed}---")

Outputs:

---   giraffe  ---
---giraffe---