Python String endswith() Method with Examples

Python String endswith() Method with Examples

Python string endswith() method is used with strings, and the method returns true if the end of the string with the specified suffix in all other cases it returns false. If the start and end index is not provided, then by default, it takes 0 and length-1 as starting and ending indexes where ending index is not included in our search.

Python string endswith() method is used with strings, and the method returns true if the end of the string with the specified suffix in all other cases it returns false. If the start and end index is not provided, then by default, it takes 0 and length-1 as starting and ending indexes where ending index is not included in our search.

Content Overview

1 Python String endswith() 1.1 Syntax 1.1.1 Programs on endswith() Method 1.1.2 Write a program including start and end suffix in the endswith() method 2 Python string endswith() With Tuple Suffix 3 Regular expression with Python endswith() 4 Python string endswith list 4.1 How to check if the last characters in a string are numbers

Python String endswith()

Python endswith() method returns the boolean.

  1. It returns True if strings end with the specified suffix.
  2. It returns False if a string doesn’t end with a particular suffix.

Syntax

string.endswith(suffix, start, end)

Suffix: This is the part of the string for which we are checking if the string is ending with that part or not.

Start: The suffix starts from here; it is optional.

End: The suffix ends here; it is optional.

Programs on endswith() Method

Write a program to show the working of the endswith() method.

# app.py

string = "Hello I am a string and I am ending with = end"

result = string.endswith("end")
result1 = string.endswith("END")
print("Comparing it with right suffix so output: ",result)
print("Comparing it with wrong suffix so output: ",result1)

See the output.

Comparing it with right suffix so output:  True
Comparing it with the wrong suffix so output:  False

Write a program including start and end suffix in the endswith() method

See the following program.

# app.py

string = "This is a string and is awesome"
right_suf = "some"
wrong_suf = "some!"
print(string.endswith(right_suf, 28, 31))
print(string.endswith(wrong_suf, 28, 31))

See the output.

False
False

Python string endswith() With Tuple Suffix

See the following code.

# app.py

text = "Jean Milburn"
result = text.endswith(('Jean', 'Otis'))

print(result)

result = text.endswith(('Jean', 'Milburn'))

print(result)

See the output.

➜  pyt python3 app.py
False
True
➜  pyt

Regular expression with Python endswith()

Python endswith() method does not allow for a regular expression. You can only search for a string suffix. This makes sense. The regular expression can describe an infinite set of matching strings. For example, the regex ‘.*A’ matches all words ending with the character ‘A.’ This operation can be computationally expensive.

Python string endswith list

If you want to check if the string ends with one of the strings from a list, then you can use the Python endswith() function.

See the following code.

# app.py

extensions = ['.jpg','.png']
file_name = 'test.png'

print(file_name.endswith(('.jpg', '.png')))

Python tuple syntax with endswith() function is the following.

file_name.endswith(tuple(extensions))

The endswith() function also accepts the tuple. So the code becomes more straightforward. See the output.

➜  pyt python3 app.py
True
➜  pyt

How to check if the last characters in a string are numbers

Now, for this kind of requirement where you need to check if the last characters in the string are numbers and return those numbers, then you need to use Python regexp.

See the following code.

# app.py

import re

string = 'eleven11'
m = re.search(r'\d+$', string)

# if the string ends in digits m will be a Match object, or None otherwise.
if m is not None:
    print(m.group())

In the above example, our string is eleven11 So, if the string contains the number, then it will return the number; otherwise, it won’t return.

See the output.

➜  pyt python3 app.py
11
➜  pyt

Finally, Python String endswith() Method Example is over.

python

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Basic Data Types in Python | Python Web Development For Beginners

In the programming world, Data types play an important role. Each Variable is stored in different data types and responsible for various functions. Python had two different objects, and They are mutable and immutable objects.

How To Compare Tesla and Ford Company By Using Magic Methods in Python

Magic Methods are the special methods which gives us the ability to access built in syntactical features such as ‘<’, ‘>’, ‘==’, ‘+’ etc.. You must have worked with such methods without knowing them to be as magic methods. Magic methods can be identified with their names which start with __ and ends with __ like __init__, __call__, __str__ etc. These methods are also called Dunder Methods, because of their name starting and ending with Double Underscore (Dunder).

Python Programming: A Beginner’s Guide

Python is an interpreted, high-level, powerful general-purpose programming language. You may ask, Python’s a snake right? and Why is this programming language named after it?

Hire Python Developers

Are you looking for experienced, reliable, and qualified Python developers? If yes, you have reached the right place. At **[HourlyDeveloper.io](https://hourlydeveloper.io/ "HourlyDeveloper.io")**, our full-stack Python development services...

Python any: How to Check If Element is Iterable or Not

Python any() function returns True if any element of an iterable is True otherwise any() function returns False. The syntax is any().