removesuffix() method - str class

The removesuffix() method of str class in Python removes a given suffix and returns the new string. If the suffix is not found the original string is returned.

Str Updated 4 Dec 2020 1 min read

Method Name

removesuffix

Method Signature

removesuffix(suffixToRemove)

Overview

  • The method removes a given suffix from str object and returns the new string.
  • If the given suffix is not found then it returns the original string.

Parameters

suffixToRemove - The suffix to be removed.

Return Value

A string containing the new value. If no suffix is found, original string is returned.

Example:

python
# Example Python program that removes a suffixfrom a string

            qty = "10 yards";

            qtyWithoutUnit = qty.removesuffix(" yards");

            print("String after removing the suffix:");

            print(qtyWithoutUnit);

Output:

Output
String after removing the suffix:

            10