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.

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

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

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

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:

Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more