removeprefix() Method - Str Class

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:

# 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:

String after the prefix is removed:
quick brown fox jumps over the lazy dog

 


Copyright 2024 © pythontic.com