Python Function Reference

str.strip

Remove characters at the beginning and the end of the string.

Python 3.13
str.strip([chars])

Parameters

charsstr | Noneoptional
The set of characters to remove; if not given, whitespace is removed.

Return value

A new string; the original is not changed.

Examples

Removing spaces on both sides

Python 3.13
print('  hello world  '.strip())
hello world

Removing a set of characters

Python 3.13
print('www.example.com'.strip('cmowz.'))
example

Details

chars is a set of individual characters, not a substring: any combination of them is removed from the edges. To remove a specific prefix or suffix, use str.removeprefix and str.removesuffix.

See also

Where to learn