Removes spaces (or other characters) from both ends of a string.
Synopsis
text.strip()
text.strip(characters)
Description
Users often type a stray space before or after an answer. strip removes spaces, tabs and new lines from the start and end.
lstrip removes from the left end only, and rstrip from the right end only.
Parameters
| Name | Meaning |
|---|---|
characters |
Which characters to remove. Optional. The default is any whitespace. |
Return value
A new str.
Example
name = " Ada "
print(f"[{name}]")
print(f"[{name.strip()}]")
print("--hi--".strip("-"))
It prints:
[ Ada ]
[Ada]
hi
Common mistake
print("a b c".strip())
It prints:
a b c
strip only touches the two ends. To remove every space, use replace(" ", "").
See also
Last updated 2 weeks ago