The Wayback Machine - http://web.archive.org/web/20220731054800/https://www.geeksforgeeks.org/python-startswith-endswidth-function/amp/

Python | startswith() and endswith() functions

Python library provides a number of built in methods, one such being startswith() and endswith() function which used in string related operations.

startswith()



Syntax

str.startswith(search_string, start, end)

Parameters :

search_string : The string to be searched.
start : start index of the str from where the search_string is to be searched.
end : end index of the str, which is to be considered for searching.

Use :

 
Returns :
The return value is binary. The functions returns True if the original Sentence starts with the search_string else False.

 

endswith()

Syntax :

str.endswith( search_string, start, end)

Parameters :

search_string : The string to be searched.
start : Start index of the str from where the search_string is to be searched.
end : End index of the str, which is to be considered for searching.

Use :

 
Returns :
The return value is binary. The functions returns True if the original Sentence ends with the search_string else False.
 
Below is the code explaining startswith() and endswidth() :




# Python code to implement startswith()
# and endswith() function.
  
str = "GeeksforGeeks"
  
# startswith()
print(str.startswith("Geeks"))
print(str.startswith("Geeks", 4, 10))
print(str.startswith("Geeks", 8, 14))
  
print("\n")
  
# endswith
print(str.endswith("Geeks"))
print(str.endswith("Geeks", 2, 8))
print(str.endswith("for", 5, 8))

Output :

True
False
True


True
False
True
Article Tags :