String ends with () in Python



Syntax :

 str.endswith (suffix, start, end) 

Parameters :

suffix: Suffix is ​​nothing but a string which needs to be checked.
start: Starting position from where suffix is ​​needed to be checked within the string.
end: Ending position from where suffix is ​​needed to be checked within the string.

NOTE. If no start and end indices are specified, then it defaults to 0 and length-1 as the start and end indices, where the end index is not included in our search.

Returns:

 It returns True if string ends with the given suffix otherwise returns False. 

CODE 1

# Python code shows it working
# .endswith () function

 

text = "geeks for geeks."

 
# returns False

result = text.endswith ( `for geeks` )

print (result)

 
# returns True

result = text.endswith ( `geeks.` )

print (result)

 
# returns True

result = text.endswith ( `for geeks.` )

print (result)

 
# returns True

result = text.endswith ( `geeks for geeks.` )

print (result)

Exit:

 False True True True 

Code 2

# Python code shows it working
# .endswith () function

 

text = " geeks for geeks. "

  
# initial parameter: 10

result = text.endswith ( `geeks.` , 10 )

print (result)

 
# Both start and end are provided
# start: 10 , end: 15
# Returns False

result = text.endswith ( `geeks` , 10 , 15 )

print result

 
# returns True

result = text.endswith ( ` geeks` , 10 , 14 )

print result

Output:

 True True False