Python offers a wide range of built-in functions and methods that make string manipulation a breeze. Among these, the `startswith()` and `endswith()` methods are valuable tools for checking the beginnings and endings of strings, respectively. In this article, we will explore how to use these methods with practical examples.
1. Introduction to `startswith()` and `endswith()`.
- Both `startswith()` and `endswith()` are string methods in Python used to determine if a given string starts or ends with a specified prefix or suffix, respectively.
- They return a Boolean value (True or False) based on the outcome of the check.
- These methods are commonly used for data validation, filtering, and conditional branching.
1.1 The `startswith()` Method.
- The `startswith()` method checks whether a string begins with a specified substring. Its syntax is as follows:
string.startswith(prefix[, start[, end]])
- `string`: The string you want to check.
- `prefix`: The substring you want to search for at the beginning of the string.
- `start` (optional): The starting index for the search. If provided, the search will begin at this index.
- `end` (optional): The ending index for the search. If provided, the search will stop before this index.
1.2 The `endswith()` Method.
- The `endswith()` method, on the other hand, checks whether a string ends with a specified substring. Its syntax is similar to `startswith()`:
string.endswith(suffix[, start[, end]])
- `string`: The string you want to check.
- `suffix`: The substring you want to search for at the end of the string.
- `start` (optional): The starting index for the search. If provided, the search will begin at this index.
- `end` (optional): The ending index for the search. If provided, the search will stop before this index.
2. Using `startswith()` with Examples.
- Let’s start by exploring the `startswith()` method with some practical examples:
2.1 Example 1: Basic Usage.
- Source code.
text = "Hello, World!" result = text.startswith("Hello") print(result) # Output: True
- In this example, the `startswith()` method checks if the string text starts with the substring “Hello“. Since it does, the method returns `True`.
2.2 Example 2: Specifying Start and End.
- You can specify the start and end positions for the search:
>>> text = "Python is great!" >>> result = text.startswith("is", 7, 13) >>> print(result) # Output: True True >>> #>>> result = text.startswith("is", 6, 13) #>>> print(result) # Output: False #False
- Here, we check if the substring “is” starts at index 7 and ends before index 13 in the string. Since it does, the method returns `True`.
2.3 Example 3: Checking for Multiple Prefixes.
- You can check if a string starts with any of several prefixes using a tuple:
>>> text = "apple banana cherry" >>> prefixes = ("apple", "orange", "banana") >>> result = text.startswith(prefixes) >>> print(result) # Output: True True >>> >>> prefixes = ( "orange", "banana") >>> >>> result = text.startswith(prefixes) >>> >>> print(result) # Output: False False
- In this case, the method returns `True` because the string starts with one of the prefixes in the tuple.
3. Using `endswith()` with Examples.
- Now, let’s dive into the `endswith()` method with some illustrative examples:
3.1 Example 1: Basic Usage.
- Source code.
filename = "document.pdf" result = filename.endswith(".pdf") print(result) # Output: True
- In this example, we check if the filename ends with the suffix “.pdf“. Since it does, the method returns `True`.
3.2 Example 2: Specifying Start and End.
- You can also specify the start and end positions for the search, similar to `startswith()`:
>>> text = "Python is great!" >>> result = text.endswith("is", 0, 10) >>> print(result) # Output: False False >>> result = text.endswith("is", 0, 9) >>> print(result) # Output: True True
- Here, we check if the substring “is” ends at index 10 in the string. Since it does not, the method returns `False`.
3.3 Example 3: Checking for Multiple Suffixes.
- Just like with `startswith()`, you can check if a string ends with any of several suffixes using a tuple:
>>> filename = "document.pdf" >>> suffixes = (".pdf", ".txt", ".doc") >>> result = filename.endswith(suffixes) >>> print(result) # Output: True True
- The method returns True because the filename ends with one of the suffixes in the tuple.
4. Conclusion.
- In this article, we have explored how to use the `startswith()` and `endswith()` methods in Python for checking prefixes and suffixes in strings.
- These methods are valuable tools for performing string validation, filtering, and conditional logic.
- By mastering these techniques, you can efficiently manipulate strings and enhance the functionality of your Python programs.