Python string removesuffix() method
Python String removesuffix() Method
Description
The removesuffix()
method was introduced in Python 3.9. It removes the specified substring from the end of a string and returns the remaining string.
Syntax
var.removesuffix(suffix)
Parameters
- suffix − The string to be removed from the end of the string.
Return Value
The
removesuffix()
method returns the string after removing the suffix.
Example
var = 'Explicit is better than implicit.'
var1 = var.removesuffix('.')
print ("Original string:", var)
print ("Removed suffix:", var1)
var = "Explicit is better than implicit."
var2 = var.removesuffix('implicit.')
print ("Original string:", var)
print ("Removed suffix:", var2)
var = 'Explicit is better than implicit.'
var3 = var.removesuffix("")
print ("Original string:", var)
print ("Removed suffix:", var3)
The above code will produce the following output:
Original string: Explicit is better than implicit.
Remove suffix: Explicit is better than implicit.
Original string: Explicit is better than implicit.
Remove suffix: Explicit is better than
Original string: Explicit is better than implicit.
Remove suffix: Explicit is better than implicit.