Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

str.strip

Removes matching characters from both ends of a string.

Version: Python 3.14; repository image, production runtime not attested

Syntax

s.strip(chars=None, /)

Removes matching characters from both ends of a string.

Interior spaces remain intact. An explicit argument supplies a set of characters rather than a substring.

With no argument, trim whitespace from both ends until a non-whitespace character is found. With xy each x or y is individually removable at an edge, yielding data. The space inside mid dle remains because it is not at an edge.

assert "xy x y yx".strip("xy ") == "" and "  x y  ".strip() == "x y"

Examples

print("xydataxy".strip("xy"))
print("  mid dle  ".strip())

Result

data
mid dle

With no argument, trim whitespace from both ends until a non-whitespace character is found. With xy each x or y is individually removable at an edge, yielding data. The space inside mid dle remains because it is not at an edge.

Sources