Python String endswith() function

Python string endswith() function returns True if the input string ends with a particular suffix, else it returns False.

Key Points:

  • Return Type: Boolean i.e. True or False
  • Parametric Values: There are 3 parameters: Suffix, Start, End
ParameterDescription
SuffixIt can be a String or Tuple of Strings that are to be checked. It is
Case Sensitive
StartIt is optional and is to specify the starting index from where the check will start
EndIt is optional and is to specify the ending index where the check will end

Python String endswith() Syntax

string.endswith(suffix[, start[, end]])


String endswith() Examples

Example 1:

str= 'Engineering Discipline'

print(str.endswith('Discipline'))  # True

Example 2: Providing offset

str = 'Engineering is an interesting discipline'

print(str.endswith('discipline', 2))  # True
print(str.endswith('Engineering', 10))  # False

Example 3: Using the len() function with the endswith() function

str = 'Engineering is an interesting discipline'

print(str.endswith('discipline', 11, len(str)))  # True
print(str.endswith('Engineering', 0, 11))  # True
print(str.endswith('Python', 8))  # False

Example 4:

str = 'C++ Java Python'

print(str.endswith(('Perl', 'Python')))  # True
print(str.endswith(('Java', 'Python'), 3, 8))  # True

Conclusion

Python String endswith() function is a utility to check if the string ends with the given suffix or not.


References