In this Python tutorial, you will learn how to check if a string ends with a specific substring or suffix string, using string.endswith() method.

Python – Check if string ends with a specific suffix

String is a sequential collection of characters. When working with Strings, we may need to check if the string ends with a specific word or in other words check if the string has a specific suffix string.

To check if a string ends with a given substring or suffix string, we can use endswith() method of the string str class.

Syntax

The syntax of endswith() method is

str.endswith(suffix[, start[, end]])

where suffix is the substring we are looking to match in the main string. start and end arguments are optional. end can be mentioned only if start is provided.

If start is given, the main string from that position is considered for matching with suffix.

If end is given, the main string till that position is considered for matching with suffix.

endswith() returns True if the string ends with the suffix, else endswith() returns False.

Note: You can also provide multiple strings as a tuple for suffix. In that case, endswith() returns true if the string ends with one of the string in suffix. We shall look into this scenario with an example.

ADVERTISEMENT

Examples

1. Check if given string ends with specific suffix string

In this example, we have taken a website url as our main string. We shall validate the string, if it ends with suffix com. To validate this, we shall use string.endswith() method as described in the syntax.

Python Program

#the string
website = 'https://www.tutorialkart.com'

#suffix
suffix = 'com'

#check if string ends with suffix
isValid = website.endswith(suffix)

print(isValid)
Try Online

Output

True

Let us check for some of the negative scenarios.

Python Program

#the string
website = 'https://www.tutorialkart.com'

#check if string ends with suffix
print(website.endswith('http'))
print(website.endswith('www'))
print(website.endswith('com'))
Try Online

Output

False
False
True

2. Check if given string ends with any of the suffixes

We have noted in the syntax section that you can specify multiple strings for suffix.

In this example, we shall take multiple strings as a tuple for suffix and use endswith() method.

Python Program

#the string
website = 'https://www.tutorialkart.com'

#suffix
suffix = ('com','in','us','org')

#check if string ends with suffix
isValid = website.endswith(suffix)

print(isValid)
Try Online

Output

True

Our string ends with one of the specified strings in suffix.

3. Check if substring of given string ends with given suffix string

In this example, we shall use the parameters start and end mentioned in the syntax above. This lets us check if the substring of given string, defined by the start index and end index, ends with the specified suffix string.

Python Program

#the string
website = 'https://www.tutorialkart.com'

#check if string ends with suffix
#start specified
print(website.endswith('com', 8)) #True
#both start and end specified
print(website.endswith('com', 8, 12)) #False
print(website.endswith('us', 15)) #False
Try Online

Output

True
False
False

Conclusion

In this Python Tutorial, we learned how to use string.endswith() method to check if a string ends with a suffix string using Python programs.