![]() |
VOOZH | about |
The endswith() method is a tool in Python for checking if a string ends with a particular substring. It can handle simple checks, multiple possible endings and specific ranges within the string. This method helps us make our code cleaner and more efficient, whether we're checking for file extensions, validating URLs or processing text.
True
Table of Content
str.endswith(suffix, start, end)
Note: If 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.
Sometimes, we might want to check if a string ends with one of several possible substrings. The endswith() method allows us to pass a tuple of substrings. It will return True if the string ends with any one of those substrings.
True
endswith() method also has optional parameters start and end which allow us to check a substring within a specific part of the string. The start parameter defines where to begin checking and the end parameter defines where to stop checking.
True
When building a file upload system, it’s important to ensure that users upload the correct types of files. For instance, if we only accept images in .jpg and .png formats, we can use endswith() to validate the file extension before processing the file.
File is valid!