Table of Contents
In this tutorial, we will learn about the Python String endswith() method with the help of examples.
The endswith() method returns True if a string ends with the specified suffix. If not, it returns False.
Example
message = 'Python is fun'
# check if the message ends with fun
print(message.endswith('fun'))
# Output: True
1. Syntax of String endswith()
The syntax of endswith() is:
str.endswith(suffix[, start[, end]])
2. endswith() Parameters
The endswith() takes three parameters:
- suffix – String or tuple of suffixes to be checked
- start (optional) – Beginning position where suffix is to be checked within the string.
- end (optional) – Ending position where suffix is to be checked within the string.
3. Return Value from endswith()
The endswith() method returns a boolean.
- It returns True if a string ends with the specified suffix.
- It returns False if a string doesn’t end with the specified suffix.
4. Example 1: endswith() Without start and end Parameters
text = "Python is easy to learn."
result = text.endswith('to learn')
# returns False
print(result)
result = text.endswith('to learn.')
# returns True
print(result)
result = text.endswith('Python is easy to learn.')
# returns True
print(result)
Output
False True True
5. Example 2: endswith() With start and end Parameters
text = "Python programming is easy to learn."
# start parameter: 7
# "programming is easy to learn." string is searched
result = text.endswith('learn.', 7)
print(result)
# Both start and end is provided
# start: 7, end: 26
# "programming is easy" string is searched
result = text.endswith('is', 7, 26)
# Returns False
print(result)
result = text.endswith('easy', 7, 26)
# returns True
print(result)
Output
True False True
6. Passing Tuple to endswith()
It’s possible to pass a tuple suffix to the endswith() method in Python.
If the string ends with any item of the tuple, endswith() returns True. If not, it returns False
7. Example 3: endswith() With Tuple Suffix
text = "programming is easy"
result = text.endswith(('programming', 'python'))
# prints False
print(result)
result = text.endswith(('python', 'easy', 'java'))
#prints True
print(result)
# With start and end parameter
# 'programming is' string is checked
result = text.endswith(('is', 'an'), 0, 14)
# prints True
print(result)
Output
False True True
If you need to check if a string starts with the specified prefix, you can use startswith() method in Python.
Related posts:
How to get current date and time in Python?
JavaScript Methods of RegExp and String
Python Variables, Constants and Literals
Python Program to Convert Kilometers to Miles
Python reversed()
Python Input, Output and Import
Python Program to Reverse a Number
Learning scikit-learn Machine Learning in Python - Raul Garreta & Guillermo Moncecchi
Python String isalpha()
Python Program to Find Factorial of Number Using Recursion
Java – Reader to String
Building Machine Learning Systems with Python - Willi Richert & Luis Pedro Coelho
Introduction to Machine Learning with Python - Andreas C.Muller & Sarah Guido
Converting a List to String in Java
Python List remove()
Python Program to Count the Number of Each Vowel
Python Exception Handling Using try, except and finally statement
Python Program to Sort Words in Alphabetic Order
Python Program Read a File Line by Line Into a List
Python Program to Compute the Power of a Number
Python Program to Print Colored Text to the Terminal
Python Namespace and Scope
Python Operator Overloading
Convert Character Array to String in Java
Python type()
Python Program to Concatenate Two Lists
Python String isspace()
Python List
Python callable()
Python List index()
Python Program to Differentiate Between type() and isinstance()
Python repr()