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

ParameterDescription
prefix The exact text to remove from the start.

Return value

A new string.

Examples

Remove a prefix if presentTry it yourself
print("www.example.com".removeprefix("www."))
print("example.com".removeprefix("www."))

Output

example.com
example.com
Clean up headingsTry it yourself
for line in ["# Title", "## Section"]:
    print(line.removeprefix("#").strip())

Output

Title
# Section

Version history

VersionChange
Python 3.9Added