Python String – removesuffix()
Last Updated :
01 Nov, 2020
If the string ends with the suffix and the suffix is not empty, the str.removesuffix(suffix, /) function removes the suffix and returns the rest of the string. If the suffix string is not found then it returns the original string. It is introduced in Python 3.9.0 version.
Syntax: str.removesuffix(suffix, /)
Parameters:
Suffix– suffix string that we are checking for.
Return value:
Returns: string[ : – len (suffix) ] if the string ends with suffix string and that suffix is not empty. Else it returns the copy of original string.
Example 1:
Python3
print ( 'ComputerScience' .removesuffix( 'Science' ))
print ( 'GeeksforGeeks' .removesuffix( 'for' ))
|
Output:
Computer
GeeksforGeeks
Example 2:
Python3
string1 = "Welcome to python 3.9"
print ( "Original String 1 : " , string1)
result = string1.removesuffix( "3.9" )
print ( "New string : " , result)
string2 = "Welcome Geek"
print ( "Original String 2 : " , string2)
result = string2.removesuffix( "Welcome" )
print ( "New string : " , result)
|
Output:
Original String 1 : Welcome to python 3.9
New string : Welcome to python
Original String 2 : Welcome Geek
New string : Welcome Geek
Share your thoughts in the comments
Please Login to comment...