Python String endswith() Method
Python String endswith() method returns True if a string ends with the given suffix, otherwise returns False.
Python String endswith() Method Syntax:
Syntax: str.endswith(suffix, start, end)
Parameters:
- suffix: Suffix is nothing but a string that needs to be checked.
- start: Starting position from where suffix is needed to be checked within the string.
- end: Ending position + 1 from where suffix is needed to be checked within the string.
Return: ReturnsTrue if the string ends with the given suffix otherwise return False.
Note: If start and end index is not provided then by default it takes 0 and length -1 as starting and ending indexes where ending index is not included in our search.
Python String endswith() Method Example
Python3
string = "geeksforgeeks"print(string.endswith("geeks")) |
Output:
True Example 1: Working of endswith() method Without start and end Parameters
we shall look at multiple test cases on how one can use Python String endswith() method without start and end parameters.
Time complexity: O(1)
Auxiliary space: O(1)
Python
text = "geeks for geeks."# returns Falseresult = text.endswith('for geeks')print (result)# returns Trueresult = text.endswith('geeks.')print (result)# returns Trueresult = text.endswith('for geeks.')print (result)# returns Trueresult = text.endswith('geeks for geeks.')print (result) |
Output:
False True True True
Time complexity: O(n), where n is the length of the string being searched for.
Auxiliary space: O(1), as the method endswith() only requires a constant amount of memory to store the variables text, result, and the string being searched for.
Example 2: Working of endswith() method With start and end Parameters
we shall add two extra parameters, the reason to add start and the end values is that sometimes you need to provide big suffix/text to be checked and that time start and end parameters are very important.
Python
# Python code shows the working of# .endswith() functiontext = "geeks for geeks."# start parameter: 10result = text.endswith('geeks.', 10)print(result)# Both start and end is provided# start: 10, end: 16 - 1# Returns Falseresult = text.endswith('geeks', 10, 16)print(result)# returns Trueresult = text.endswith('geeks', 10, 15)print(result) |
Output:
True True False
Time complexity: O(1) for each call to the endsWith() function.
Auxiliary space: O(1) as no extra space is required apart from the given string and the variables used in the code.
Example 3: Real-World Example where endswith() is widely used
In this example, we take a String input from the user and check whether the input String endswith ‘@geeksforgeeks.org’ or not, then we print ‘Hello Geek’ else we print ‘Invalid, A Stranger detected’.
Python3
print("***Valid Geeksforgeeks Email Checker***\n")user_email = input("Enter your GFG Official Mail:").lower()if user_email.endswith("@geeksforgeeks.org"): print("Hello Geek")else: print("Invalid, A Stranger detected") |
Output:
***Valid Geeksforgeeks Email Checker*** Enter your GFG Official Mail:s@geeksforgeeks.org Hello Geek ***Valid Geeksforgeeks Email Checker*** Enter your GFG Official Mail:s@google.com Invalid, A Stranger detected
Time complexity: O(1)
Auxiliary space: O(1)


Please Login to comment...