Built with Tutify.io

    AIStudyLabs

    Python String Startswith

    Python String Startswith

    Python startswith() method is indeed used to check whether a given string starts with a specified prefix. It returns True if the string starts with the specified prefix otherwise, it returns False.

    In this article, I will explain the Python string startswith() method and using its syntax, parameters, and usage how we can check whether a specified string starts with a given prefix or substring with examples.

    <a id="h-1-quick-examples-of-string-startswith"></a>

    1. Quick Examples of String Startswith

    If you are in a hurry, below are some quick examples of string startswith() method.

    
    # Quick examples of string startswith
    
    # Initialization string
    string = "Welcome To AIStudyLabs"
    
    # Example 1: Check if the string starts with "Welcome"
    result = string.startswith("Welcome")
    
    # Example 2: Check if the string starts 
    # With "AIStudyLabs"
    result = string.startswith("AIStudyLabs")
    
    # Example 3: Check if the substring starting 
    # From index 12 starts with "Spark"
    result = string.startswith("Spark", 12)
    
    # Example 4: Check if the substring starting 
    # From index 11 and ending at index 17 starts with "SparkBy"
    result = string.startswith("SparkBy", 11, 18)
    
    # Example 5: Check if the string starts 
    # With any of the prefixes in the tuple
    result = string.startswith(("Welcome", "To", "AIStudyLabs"))
    

    <a id="h-2-python-string-startswith-method"></a>

    2. Python String startswith() Method

    The startswith() method in Python returns True if a string starts with the specified prefix otherwise, it returns False.

    2.1 Syntax of String startswith() Method

    Following is the syntax of the string startswith() method.

    
    # Syntax of string.startswith() function
    string.startswith(prefix, start, end)
    

    2.2 Parameter of String startswith()

    2.3 Return Value

    It returns a boolean value.

    <a id="h-3-use-startswith-without-start-and-end-parameters"></a>

    3. Use startswith() Without Start and End Parameters

    You can use the startswith() method without the start and end parameters to check if a string starts with a specific prefix.

    In the below example, the string variable is initialized with the value "Welcome To AIStudyLabs". Apply this method to the string variable to check if it starts with the substring "Welcome". The result of this check is stored in the result variable. The print() function is used again to display the value of the result variable. This method returns True because the string does indeed start with the prefix "Welcome".

    
    # Initialization the string
    string = "Welcome To AIStudyLabs"
    print("Original string:", string)
    
    # Check if the string starts with "Welcome"
    result = string.startswith("Welcome")
    print("Check the string starts with specified starts:",result)
    

    Yields below output.

    Another example is that you can check if a string starts with the specified prefix. For example, the string variable is initialized with the value "Welcome To AIStudyLabs" and then applied the startswith() method over the string variable to check if it starts with the substring "AIStudyLabs". The result of this check is stored in the result variable.

    In the below example, this method returns False because the string does not start with the prefix "AIStudyLabs".

    
    # Initialization string
    string = "Welcome To AIStudyLabs"
    print("Original string:", string)
    
    # Check if the string starts 
    # With "AIStudyLabs"
    result = string.startswith("AIStudyLabs")
    print ("Check the string starts with specified starts:",result)
    

    Yields below output.

    <a id="h-4-use-startswith-with-start-and-end-parameters"></a>

    4. Use startswith() With Start and End Parameters

    An alternative way to check if a string starts with a specified prefix is by using startswith()method with the start parameter. In the following program, we are checking if the substring starts from the index 12 (inclusive) using the startswith() method. Here, the substring is "Spark". Its corresponding index is 11 since index starts from 0 in Python. This function returns False. Because the string does not start with the specified prefix "Spark".

    
    # Initialization string
    string = "Welcome To AIStudyLabs"
    print("Original string:", string)
    
    # Check if the substring starting 
    # From index 12 starts with "Spark"
    result = string.startswith("Spark", 12)
    print("Check with start parameter:", result)
    
    # Output:
    # Original string: Welcome To AIStudyLabs
    # Check with start parameter: False
    

    Similarly, you can use the startswith() method with both the start and end parameters. In this program, you're checking if the substring starting from index 11(inclusive) and ending at index 18(exclusive) starts with "SparkBy". The substring in this range is "SparkBy", which does match the specified prefix, so the result is True.

    
    # Initialization string
    string = "Welcome To AIStudyLabs"
    print("Original string:", string)
    
    # Check if the substring starting 
    # From index 11 and ending at index 18(exclusive) starts with "SparkBy"
    result = string.startswith("SparkBy", 11, 18)
    print("Check with start and end parameters:", result)
    
    # Output
    # Original string: Welcome To AIStudyLabs
    # Check with start and end parameters: True
    

    <a id="h-5-check-if-a-string-starts-with-a-substring"></a>

    5. Check if a String Starts with a Substring

    To check if the string starts with a specified tuple prefix, use the startswith() method. If the string starts with any of the items in the tuple, the method will return True.

    In the below example, apply this method over the string and check whether the string starts with any of the prefixes in the tuple ("Welcome", "To", "AIStudyLabs"). Since the string does indeed start with "Welcome", the result is True.

    
    # Initialization string
    string = "Welcome To AIStudyLabs"
    print("Original string:", string)
    
    # Check if the string starts 
    # With any of the prefixes in the tuple
    result = string.startswith(("Welcome", "To", "AIStudyLabs"))
    print("Check the string starts with specified starts:", result)
    
    # Output:
    # Original string: Welcome To AIStudyLabs
    # Check the string starts with specified starts: True
    

    <a id="h-conclusion"></a>

    Conclusion

    In this article, I have explained the Python string startswith() method and using its syntax, parameters, and usage how we can check the string starts with a specified prefix. And also explained how it returns a boolean value like True or False when the given string satisfies with a specified prefix.

    Happy Learning !!

    <a id="h-related-articles"></a>

    Related Articles