endswith() Python

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.
Transform Your Career
Choose from our industry-leading programs designed for career success
Modern Software and AI Engineering Program
Master full-stack development with AI integration
+1000 moreModern Data Science and ML with specialisation in AI
Advanced data science techniques with AI specialization
+1000 moreAdvanced AIML with Specialisation in Agentic AI
Deep dive into AIML with focus on Agentic systems
+1000 moreDevOps, Cloud & AI Platform Engineering
Build and manage AI-powered cloud infrastructure
+1000 moreAI Engineering Advanced Certification by IIT-Roorkee
Premier AI engineering certification from IIT-Roorkee
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.
Turn Learning into Career Growth
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.