removeprefix() Method - Str Class

The method removeprefix() of str class in Python, removes a given prefix from a string and returns the modified string.

Str Updated 3 Dec 2020 1 min read

Method Name

removeprefix

Method Signature

removeprefix(prefixToRemove)

Overview

  • The method removeprefix() removes the given prefix from a string and returns the original string.
  • If the given prefix is not found the method returns the original string.

Parameters

prefixToRemove - A str object containing the prefix to be removed

Return Value

Returns a str object containing the modified string.

Example

python
# Example Python program that removes a

            # given prefix from a string

            sentence         = "The quick brown fox jumps over the lazy dog";

            prefixRemoved     = sentence.removeprefix("The ");

            print("String after the prefix is removed:");

            print(prefixRemoved);

Output

Output
String after the prefix is removed:

            quick brown fox jumps over the lazy dog