Python Glossary Русский

all Python 3Strings (str)function

str.strip

stripping

Return a copy with leading and trailing characters removed (whitespace by default).

Syntax

s.strip([chars])

strip removes any characters from the given set at both ends — not a substring; order and duplicates inside the set are irrelevant, and the middle of the string is never touched. Bare strip() clears all whitespace, newlines and tabs included, which is why it is routinely applied to input() and to lines read from a file. Strings are immutable, so s.strip() on a line of its own does nothing; you have to assign the result.

Examples

print('  hi  '.strip())    # → hi
print('xxhixx'.strip('x'))  # → hi
print('...a.'.strip('.'))   # → a

See also

Open in the full glossarydocs.python.orgReport an inaccuracy