YouTip LogoYouTip

Python3 String Startswith

## Python3 String startswith() Method In Python, the `startswith()` method is a built-in string method used to check whether a string begins with a specified prefix. It returns `True` if the string starts with the given substring; otherwise, it returns `False`. Additionally, you can specify an optional search range using start and end indices to check a specific slice of the string. --- ## Syntax The syntax for the `startswith()` method is as follows: ```python str.startswith(prefix, start=0, end=len(string)) ``` ### Parameters * **`prefix`**: The substring (or a tuple of substrings) to search for at the start of the string. * **`start`** *(Optional)*: The index position where the search begins. The default value is `0` (the beginning of the string). * **`end`** *(Optional)*: The index position where the search ends. The default value is the length of the string. ### Return Value * Returns **`True`** if the string (or the specified slice of the string) starts with the specified prefix. * Returns **`False`** if it does not. --- ## Code Examples ### Example 1: Basic Usage and Slicing The following example demonstrates how to use `startswith()` with and without the optional `start` and `end` parameters: ```python #!/usr/bin/python3 # Define the target string text = "this is string example....wow!!!" # Check if the string starts with 'this' print(text.startswith('this')) # Output: True # Check if the substring starting from index 8 starts with 'string' print(text.startswith('string', 8)) # Output: True # Check if the substring from index 2 to 4 starts with 'this' print(text.startswith('this', 2, 4)) # Output: False ``` **Output:** ```text True True False ``` --- ## Advanced Considerations & Best Practices ### 1. Passing a Tuple of Prefixes The `prefix` parameter can also accept a **tuple** of strings. If the string starts with *any* of the elements in the tuple, the method returns `True`. This is highly useful for checking multiple potential prefixes in a single call. ```python filenames = ["report.pdf", "image.png", "document.docx", "archive.zip"] for file in filenames: # Check if the file starts with 'rep' or 'doc' if file.startswith(('rep', 'doc')): print(f"Found matching document: {file}") ``` **Output:** ```text Found matching document: report.pdf Found matching document: document.docx ``` ### 2. Case Sensitivity The `startswith()` method is **case-sensitive**. For example, `"Python".startswith("py")` will return `False`. If you need to perform a case-insensitive check, convert the string to lowercase first: ```python text = "Python Programming" # Case-sensitive check print(text.startswith("py")) # Output: False # Case-insensitive check print(text.lower().startswith("py")) # Output: True ``` ### 3. Empty String Check If you pass an empty string `""` as the prefix, `startswith()` will always return `True`. ```python text = "Hello World" print(text.startswith("")) # Output: True ```
← Python3 String TranslatePython3 String Rjust β†’