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.
Arguments
Parameters
| Name | Description |
|---|---|
| prefix | The exact starting text to remove. |
Result
Returns
A new string with the prefix removed, or the original text if it does not start with that prefix.
Run it
Try it
Run it and change it
text = "unhappy"
print(text.removeprefix("un")) Output
Press Run to see what it prints.Worth knowing
- removeprefix() vs lstrip():
removeprefix('Test_')removes the exact substring'Test_', whereaslstrip('Test_')strips any leading combination of'T','e','s','t', and'_'.