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 :
- 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.
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 :
- 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.
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
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- String endswith() in Python
- Python | Pandas Series.str.endswith()
- Python | Numpy np.char.endswith() method
- Python | String startswith()
- Python | Pandas Series.str.startswith()
- Numpy string operations | startswith() function
- Mathematical Functions in Python | Set 2 (Logarithmic and Power Functions)
- Mathematical Functions in Python | Set 3 (Trigonometric and Angular Functions)
- Mathematical Functions in Python | Set 4 (Special Functions and Constants)
- Mathematical Functions in Python | Set 1 (Numeric Functions)
- Complex Numbers in Python | Set 2 (Important Functions and Constants)
- Complex Numbers in Python | Set 3 (Trigonometric and Hyperbolic Functions)
- Statistical Functions in Python | Set 1 (Averages and Measure of Central Location)
- Python | Set 2 (Variables, Expressions, Conditions and Functions)
- Python bit functions on int (bit_length, to_bytes and from_bytes)
- Difference between input() and raw_input() functions in Python
- Python - Invoking Functions with and without Parentheses
- turtle.setpos() and turtle.goto() functions in Python
- Why and how are Python functions hashable?
- Python Regex - re.MatchObject.start() and re.MatchObject.end() functions
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : z00003


