Python Strings: startswith() Method

1. What Is the startswith() Function in Python?

The startswith() function is a built-in Python string method used to check whether a string begins with a specific prefix. It returns a Boolean value — True if the string starts with the given substring, and False otherwise. This method is widely used when validating data formats, checking file extensions, matching URL protocols (like https), or verifying any pattern that must appear at the beginning of a string.

Python startswith() Method: Syntax, Parameters & Return Value

Python max() Method Syntax


str.startswith(prefix[, start[, end]])

5. Examples of Using the startswith() Function in Python

Below are practical, easy-to-understand examples that demonstrate how the startswith() method behaves in different situations.

Example 1: Basic usage with a single prefix


text = "Hello, world!" print(text.startswith("Hello")) print(text.startswith("world")) # Output: True False
Explanation:

The string begins with “Hello”, so the result is True. It does not start with “world”, hence False.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top