Python Glossary Русский

Python 3.9+Strings (str)function

str.removeprefix

replacement

Return a copy with the given prefix removed if present, otherwise unchanged (Python 3.9+).

Syntax

s.removeprefix(prefix)

The classic mix-up is with lstrip: lstrip('test_') peels off any leading characters from the set t, e, s, _ in any order, while removeprefix matches that exact substring. At most one copy of the prefix is removed, never repeated occurrences. It only exists from Python 3.9 on.

Examples

print('test_foo'.removeprefix('test_'))  # → foo
print('foo'.removeprefix('test_'))       # → foo
print('__x'.removeprefix('_'))           # → _x

See also

Open in the full glossarydocs.python.orgReport an inaccuracy