endswith() Python

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

Overview

The endswith() method of String in python returns True if a string ends with the given suffix otherwise returns False.

Syntax for endswith() function in Python

The syntax of the endswith() method in Python is:

Where str is the name of the string that will be checked.

Parameters for endswith() function in Python

  • str: Name of the string that will be checked based on suffix entered.
  • suffix: This is a required argument. It is the string that is needed to be checked or looked for.
  • start: It is an optional argument. It is the starting position from where suffix is needed to be checked within the string. In order words, it is the position from where slicing begins.
  • end: It is also an optional argument, and it is the ending index at which the search should end. By default, the end is the last index of the string.

NOTE: If start and end index is not provided then by default it takes 0 and length of string -1 respectively where ending index is exclusive in the search.

Return Values for endswith() function in Python2

Return Type: bool

The endswith() method will return a bool value like True or False.

endswith() will return True if the string matches with the entered suffix, otherwise False.

Example for endswith() function in Python

Code:

Output:

What is endswith() function in Python?

Python’s str class provides several methods to perform some common operations on a string.

One such method is endswith() string method. endswith() method is used to check whether the string ends with the given suffix. If the suffix matches with the string, then the endswith() method will return True, otherwise False.

Note:

  • The endswith() method will return a bool value.
  • The parameter suffix in this method is case-sensitive.
  • In the suffix parameter, we can pass a tuple of strings instead of a single string.
    • If we have passed a tuple of strings, then if any of the elements of the tuple matches the string, then also the endswith() will return True.

Note: Case-sensitive means if the suffix content is in lower-case, and the string content is upper-case, then the method endswith() will also return False.

More Examples

Example 1: Working of endswith() Method without Start and End Parameters

Code:

Output:

Example 2: Working of endswith() Method with Start and End Parameters

Code:

Output:

Example 3: Working of endswith() Method with Tuple

Code:

Output:

Conclusion

  • The endswith() method checks whether the given string ends with the suffix.
  • suffix parameter is case-sensitive.
  • endswith() method does not affect the original string because the endswith() method returns a bool value of either True or False.

See Also: