When working with strings in Python programming, we may wish to check whether a string ends with a specific value or not. For example, suppose we are writing a program that checks to see if a user’s name ends with a specific letter in order to carry out a special lottery event or distribute a discount coupon to them. This is where the Python endswith() function comes into play. Let us dive deep into this article to understand the Python endswith() concept with few programming examples.
Definition
- Python endswith() is a string method that returns True if the input string ends with the specified suffix(string); else it returns False.
- The Python endswith() string function is used to check whether the input string ends with the specified string; optionally restricting the matching with given start and end parameters.
Python endswith() Syntax
The Python endswith() function follows the below-mentioned syntax:
string.endswith(suffix, start, end)
Note – This function is case-sensitive. If you are looking for ‘p’ then it will only search for instances of a lowercase ‘p’.
endswith() Parameters
Python endswith() accepts only 3 parameters. They are as follows:
- suffix – the substring or a tuple of the substring to be checked within the string
- start (Optional) – index position at which the search for the suffix will be started. The default value is 0
- end (Optional) – index position till which the search for the suffix will be ended. The default value is the end of the string
Return value from endswith()
Python endswith() returns the following Boolean values:
- True – if the input string starts with the specified suffix
- False – if the input string does not start with the specified suffix
Example 1: endswith() Without start and end Parameters
Let us look at few basic examples to understand the working of Python endswith().
Example
# Python program to illustrate endswith()
text = 'Jerry loves to eat cheese'
print('Original string:', text)
result = text.endswith('cheese')
print('String ends with - cheese? :', result)
result = text.endswith('eat')
print('String ends with - eat? :', result)
# case-sensitive
result = text.endswith('Cheese')
print('String ends with - Cheese? :', result)
result = text.endswith('to eat cheese')
print('String ends with - to eat cheese? :', result)
Output
Original string: Jerry loves to eat cheese
String ends with - cheese? : True
String ends with - eat? : False
String ends with - Cheese? : False
String ends with - to eat cheese? : True
Example 2: endswith() With start and end Parameters
The start and end arguments limit the number of times a suffix in a string is checked as an index. An index begins at 0, so the first character’s index is 0, the second character’s index is 1, and so on. If the end parameter is not specified, it will search until it reaches the end of a string.
Example
# Python program to illustrate endswith()
text = 'This trip is going to be very exciting'
print('Original string:', text)
# specifying start parameter
result = text.endswith('very exciting', 25)
print('String ends with - very exciting? :', result)
# specifying start & end parameter
result = text.endswith('trip is going', 0, 18)
print('String ends with - trip is going? :', result)
result = text.endswith('exciting', 30, 35)
print('String ends with - exciting? :', result)
Output
Original string: This trip is going to be very exciting
String ends with - very exciting? : True
String ends with - trip is going? : True
String ends with - exciting? : False
Passing Tuple to endswith()
Python allows you to supply a tuple of suffixes to the endswith() method. The endswith() returns True if the string ends with any one of the tuple’s items. If it does not, it returns False. Let us look at the below example to understand this concept.
Example 3: endswith() With Tuple Suffix
Example
s = 'Mobile phones are handy'
result = s.endswith(('useful', 'handy', 'desirable', 'sleek'))
# prints True
print(result)
# prints False
result = s.endswith(('TV', 'Laptop', 'Mobile', 'Watch'))
print(result)
# with start & end parameter
result = s.endswith(('we', 'they', 'are handy'), 14, 25)
print(result)
Output
True
False
True
Frequently Asked Questions
Q1. What is endswith in Python?
When working with strings in Python programming, we may wish to check whether a string ends with a specific value or not. This is where the Python endswith() function comes into play. Python endswith() is a string method that returns True if the input string ends with the specified suffix(string); else it returns False. Also, the Python endswith() function is case-sensitive. If you are looking for ‘p’ then it will only search for instances of a lowercase ‘p’.
Q2. How do you use endswith in Python?
The Python endswith() string function is used to check whether the input string ends with the specified string; optionally restricting the matching with given start and end parameters. The Python endswith() function follows the below-mentioned syntax:
string.endswith(suffix, start, end)
Example
txt = 'Hello buddy, welcome back'
result = txt.endswith('back')
print(result)
result = txt.endswith('welcome back', 13)
print(result)
result = txt.endswith('buddy', 6, 15)
print(result)
Output
True
True
False
Q3. Difference between startswith() and endswith() in Python?
- Python startswith() – The startswith() function determines whether a provided substring/prefix begins with a specific string.
- Python endswith() – The endswith() function determines whether a provided substring/suffix ends with a specific string.
Example
txt = 'Venice is a beautiful travel destination'
# startswith()
result = txt.startswith('Venice')
print(result)
# endswith()
result = txt.endswith('destination')
print(result)
result = txt.endswith('Venice')
print(result)
Output
True
True
False
Leave a Reply