Saturday, October 4, 2025
HomeLanguagesPython | startswith() and endswith() functions

Python | startswith() and endswith() functions

Python library provides a number of built-in methods, one such being startswith() and endswith() functions which are 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.

Returns :

The return value is boolean. The functions returns

True

if the original Sentence starts with the search_string else

False

Use :

  • startswith() function is used to check whether a given Sentence starts with some particular string.
  • Start and end parameter are optional.
  • We may use them when we want only some particular substring of the original string to be considered for searching.

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.

Returns :

The return value is boolean. The functions returns

True

if the original Sentence ends with the search_string else

False

Use :

  • endswith() function is used to check whether a given Sentence ends with some particular string.
  • Start and end parameter are optional.
  • We may use them when we want only some particular substring of the original string to be considered for searching.

Python3




# Python code to implement startswith()
# and endswith() function.
 
str = "Lazyroar"
 
# 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

RELATED ARTICLES

Most Popular

Dominic
32332 POSTS0 COMMENTS
Milvus
85 POSTS0 COMMENTS
Nango Kala
6703 POSTS0 COMMENTS
Nicole Veronica
11868 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11929 POSTS0 COMMENTS
Shaida Kate Naidoo
6819 POSTS0 COMMENTS
Ted Musemwa
7080 POSTS0 COMMENTS
Thapelo Manthata
6775 POSTS0 COMMENTS
Umr Jansen
6776 POSTS0 COMMENTS