YouTip LogoYouTip

Att String Endswith

## Python String endswith() Method The `endswith()` method in Python is a built-in string method used to determine whether a string ends with a specified suffix. It returns `True` if the string ends with the specified suffix, and `False` otherwise. This method is highly efficient for file extension checks, URL routing, and general text processing. --- ## Syntax The syntax for the `endswith()` method is as follows: ```python str.endswith(suffix[, start[, end]]) ``` ### Parameters * **`suffix`**: *Required.* This can be a string (e.g., `".py"`) or a **tuple** of strings (e.g., `(".py", ".txt", ".md")`). If a tuple is provided, the method returns `True` if the string ends with *any* of the elements in the tuple. * **`start`**: *Optional.* The index from which the search starts within the string. Default is `0` (the beginning of the string). * **`end`**: *Optional.* The index at which the search ends within the string. Default is the end of the string. ### Return Value * Returns **`True`** if the string ends with the specified suffix. * Returns **`False`** if the string does not end with the specified suffix. --- ## Code Examples ### 1. Basic Usage The following example demonstrates how to check if a string ends with a specific substring. ```python # Define a sample string text = "this is string example....wow!!!" # Check if the string ends with "wow!!!" suffix = "wow!!!" print(text.endswith(suffix)) # Output: True # Check with a start index (starts searching from index 20) print(text.endswith(suffix, 20)) # Output: True # Check another suffix with start and end boundaries suffix = "is" # Search within the slice text[2:4] -> "is" print(text.endswith(suffix, 2, 4)) # Output: True # Search within the slice text[2:6] -> "is i" print(text.endswith(suffix, 2, 6)) # Output: False ``` **Output:** ```text True True True False ``` --- ### 2. Matching Multiple Suffixes (Using a Tuple) You can pass a tuple of strings to the `suffix` parameter. This is extremely useful when you want to check for multiple possible endings (e.g., different file extensions) at once. ```python filename = "document.pdf" # Check if the file is an image or a PDF # Note: The suffixes must be passed as a tuple, not a list is_document = filename.endswith((".pdf", ".docx", ".txt")) is_image = filename.endswith((".png", ".jpg", ".jpeg")) print(f"Is document: {is_document}") # Output: True print(f"Is image: {is_image}") # Output: False ``` **Output:** ```text Is document: True Is image: False ``` --- ## Key Considerations 1. **Case Sensitivity**: The `endswith()` method is case-sensitive. For example, `"file.PDF".endswith(".pdf")` will return `False`. To perform a case-insensitive check, convert the string to lowercase first using `.lower()`: ```python filename = "REPORT.PDF" print(filename.lower().endswith(".pdf")) # Output: True ``` 2. **Tuple vs. List**: When checking for multiple suffixes, you **must** pass them as a `tuple`. Passing a `list` (e.g., `[".png", ".jpg"]`) will raise a `TypeError`. 3. **Slicing Behavior**: The `start` and `end` parameters behave exactly like Python's string slicing (`string[start:end]`). The character at the `end` index is excluded from the search.
← Att String ExpandtabsAtt String Encode β†’