Python 3 reference · String Methods
Python String removeprefix() Method
Return the string without the given prefix if it starts with it; otherwise return it unchanged.
Python 3 Since 3.9
Syntax
str.removeprefix(prefix)
Parameters
| Parameter | Description |
|---|---|
prefix |
The exact text to remove from the start. |
Return value
A new string.
Examples
print("www.example.com".removeprefix("www."))
print("example.com".removeprefix("www."))
Output
example.com example.com
for line in ["# Title", "## Section"]:
print(line.removeprefix("#").strip())
Output
Title # Section
Version history
| Version | Change |
|---|---|
| Python 3.9 | Added |
Related
str.removesuffix()· Return the string without the given suffix if it ends with it; otherwise return it unchanged.str.lstrip()· Return a copy with leading characters removed: whitespace by default, or any of the characters you pass.str.startswith()· Return True if the string starts with the given prefix. Pass a tuple to check several prefixes at once.- Python Strings · tutorial