Check if String Ends with a Specific Substring (Suffix)

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

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

In this tutorial, we shall learn how to check if a string ends with a specific substring or suffix.

Syntax

The syntax of endswith() method is

string.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.

Examples

Check if 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.

Example.py

#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.

Example.py

#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

Check if 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.

Example.py

#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.

Check if String Ends with Suffix considering [start, end]

In this example, we shall use the parameters start and end mentioned in the syntax above.

Example.py

#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

Conclusion

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