Menu Close

Python String endswith() Method

Python String endswith() Method

In this article, you will learn everything about the Python string endswith() method to check whether a string ends with a particular string or not along with various examples. In the previous tutorial, we have seen the Python string count() method.

Python endswith() method:

Python endswith() method is a string method which is only working with string. Python endswith() method is used to return True if the string end with a specific character otherwise, return False.

Syntax:

The syntax of Python string endswith() is:-

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

Parameters:

Python string endswith() method takes three parameters:

suffix:- string or tuple of the suffix to be checked.
start:- ( Optional ) start parameter refers to the start position where the suffix is to be checked.
end:- ( Optional ) end parameter refer to the end position where suffix check will end.

Return Value from endswith():

The Python string endswith function returns a Boolean value:

  • string endswith function returns True if the string end with a specific string.
  • string endswith function returns False if the string does not end with a specific string.

Python string endswith() Method Example:

In this example section, I am going to explore the Python endswith() method with different-different examples so that you can get more understanding of the string endswith() function in Python.

Example: Using endswith() without start and end params


str = "Programming Funda is the best place to learn to code."
print("Old string:- ", str)

str2 = str.endswith('learn coding.')

print(str2)
str2 = str.endswith('learn coding')
print(str2)

Output

Old string:-  Programming Funda is the best place to learn to code.
False
False

Example: Using endswith() with start and end params

In this example, start and end parameres have been passed to endswith() method. The start parameter represents the position from where start the search and the end represents the position where the search will end.

str = "Programming Funda is the best place to learn to code."
print("Old string:- ", str)

str2 = str.endswith('.', 0, 17)
print(str2)

str3 = str.endswith('.', 40, 50)
print(str3)

Output

Old string:-  Programming Funda is the best place to learn to code.
False
False

Example: passing tuple to endswith()

As we know that, endswith() accepts a tuple to check whether a string ends with any item of the tuple or not. If the string ends with any item of the tuple then it will return True otherwise it will return False. As you can see in the below example, I have passed a tuple (‘Programming’, ‘Coding’, ‘.’) which contained the three items to check.


str = "Programming Funda is the best place to learn to code."

str2 = str.endswith(('Programming', 'Coding', '.'))
print(str2)

str3 = str.endswith(('Programming', 'Coding'))
print(str3)

Output

True
False

Example: Passing tuple suffixes along with end and start parameter to endswith() method

string = "Best platform of learn coding is:- Programming Funda"

# printing False
tpl1 = ('Coding', 'Python')
print(string.endswith(tpl1))


# printing True

tpl2 = ('Programming', 'Funda')
print(string.endswith(tpl2))

# using start and end parameter
print(string.endswith(tpl2, 47, 52))

Output

False
True
True

Conclusion:

In this tutorial, you have seen all about the Python string endswith() method to check whether a string ends with a specific string or not.
I hope this string method will help you.

If you found this article helpful, please share and keep visiting for further Python interesting tutorials.

You can explore more Python string method from here.

For More Information:- Click Here

Python String partition() Method
Python String isupper() Method

Related Posts