Skip to content
String Methods

removeprefix()

Remove one exact prefix if it is present.

str.removeprefix(prefix, /)

str.removeprefix() (Python 3.9+) returns a copy of the string with the specified exact prefix removed if present.

Parameters

NameDescription
prefixThe exact starting text to remove.

Returns

A new string with the prefix removed, or the original text if it does not start with that prefix.

Try it

Run it and change it
text = "unhappy"
print(text.removeprefix("un"))

Worth knowing

  • removeprefix() vs lstrip(): removeprefix('Test_') removes the exact substring 'Test_', whereas lstrip('Test_') strips any leading combination of 'T', 'e', 's', 't', and '_'.

Related entries