Skip to content
String Methods

strip()

Remove whitespace or chosen characters from both ends.

str.strip(chars=None)

str.strip() returns a copy of the string with leading and trailing characters removed (whitespace by default).

Parameters

NameDescription
charsOptional characters to remove from both ends; it is not a literal prefix or suffix.

Returns

A new string with matching leading and trailing characters removed.

Try it

Run it and change it
text = "  hello  "
print("[" + text.strip() + "]")

Worth knowing

  • Character set behavior: '#hello!'.strip('#!') strips both # and ! from both ends.

Related entries