Python String endswith() Method

The endswith() function returns True if a string ends with the specified suffix (case-sensitive), otherwise returns False. A tuple of string elements can also be passed to check for multiple options. If a string ends with any element of the tuple then the endswith() function returns True.

It checks for suffix from starting index to ending index position. Index starts from 0. By default, it checks the whole string if starting and ending index is not specified.

Synatx:

str.endswith(suffix, start, end)

Parameters:

  1. suffix : (Required) String or tuple of strings to look for.
  2. start : (Optional) Starting index at which search should start. Defaults to 0.
  3. end : (Optional) Ending index at which the search should end. Defaults to the last index of a string.

Return Value:

Returns True if the string ends with the specified suffix, otherwise returns False.

The following example demonstrates the endswith() method.

Example: endswith()
mystr = 'Python is a  programming language.'

print(mystr.endswith('.')) # returns True
print(mystr.endswith('age.')) # returns True
print(mystr.endswith('language.')) # returns True
print(mystr.endswith(' programming language.')) # returns True
print(mystr.endswith('age')) # returns False, . is missing
Output
True
True
True
True
False

The following example demonstrates the use of start and end parameters.

Example: endswith()
mystr = 'Hello World'
print(mystr.endswith('h', 0, 1)) # is 'H' endswith 'h'? False
print(mystr.endswith('H', 0, 1)) # is 'H' endswith 'H'? True
print(mystr.endswith('e', 0, 2)) # is 'He' endswith 'e'? True
print(mystr.endswith('o', 0, 5)) # is 'Hello' endswith 'o'? True
Output
False
True
True
True

In the above example, the mystr.endswith('h', 0, 1) returns False because it performs case-sensitive search. The mystr.endswith('e', 0, 2) searching starts from 0 and ends index 1 (ending index -1), so it consider 'He' string which ends with 'e', so returns True.

The following example contains tuples as parameters.

Example: endswith() with tuple
mystr = 'Tutorials Teacher is a free online tutorials website'
print(mystr.endswith(('Tutorials','free','website'))) # returns True
print(mystr.endswith(('Tutorials','free')))  # returns False
print(mystr.endswith(('Tutorials','coding'), 7, 11)) # returns False
mystr.endswith(('Tutorials','coding'), 0, 9) # returns True
Output
True
False
False
True

In the above example, the mystr.endswith(('Tutorials','free','website')) checks whether a string ends with any element of the specified tuple ('Tutorials', 'free', 'website'). mystr ends with 'website', so it returns True. The mystr.endswith(('Tutorials','coding'), 7, 11) checks suffix from starting index 7 to ending index 10 (11-1).

The endswith() function will always return True if an empty string is passed as a parameter.

Example:
mystr = 'Hello World'
print(mystr.endswith(''))
print(mystr.endswith('', 1, 3))
Output
True
True
Want to check how much you know Python?