Python Function Reference

str.removeprefix

Remove the given beginning from the string if it is there.

Python 3.13
str.removeprefix(prefix)

Parameters

prefixstr
The beginning of the string to remove.

Return value

A new string; the original is not changed.

Examples

Removing a prefix from a name

Python 3.13
print('test_login'.removeprefix('test_'))
login

Difference from lstrip

Python 3.13
print('abacus'.removeprefix('ab'), 'abacus'.lstrip('ab'))
acus cus

Details

Unlike str.lstrip, it removes exactly the substring, not a set of characters. If the string starts differently, it is returned unchanged. The method was added in Python 3.9.

See also