Python string strip() method
Remove spaces (or other characters) from both ends.
Syntax
str.strip(chars=None)
The everyday way to clean text from input and files, which often has extra spaces or a new line at the end. Give chars to remove other characters. Strings cannot change, so this returns a new string; keep it with s = s.strip(…).
Parameters
| chars | the characters to remove; default: whitespace |
Return value
str
Example
Code
raw = " Sita Gurung \n" print(repr(raw.strip())) print("xxhixx".strip("x")) print("...done!!".strip(".!"))
Output
'Sita Gurung' hi done
Good to know. strip only touches the ends:
"a b".strip() keeps the middle spaces.Related
Learn it step by step in the free lesson Strings, with exercises that check your answers.