str.startswith()
Added in v3.9 · Updated March 13, 2026 · String Methods
stdlib string methods
The .startswith() method checks whether a string begins with a specified substring or one of several possible prefixes. It returns a boolean value, making it useful for conditional logic in validation, parsing, and text processing.
Syntax
str.startswith(prefix[, start[, end]])
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
prefix | str or tuple | — | The substring or tuple of substrings to check for at the start of the string |
start | int | 0 | Starting position for the search |
end | int | len(string) | Ending position for the search |
Returns: bool — True if the string starts with the prefix, False otherwise.
Examples
Basic usage
text = "hello world"
print(text.startswith("hello"))
# True
print(text.startswith("world"))
# False
Checking multiple prefixes
message = "GET /api/users HTTP/1.1"
# Check HTTP method
if message.startswith(("GET", "POST", "PUT", "DELETE")):
print("This is an HTTP request")
# This is an HTTP request
Using start and end parameters
url = "https://example.com/page"
# Check prefix within a slice
print(url.startswith("https", 0, 5))
# True
# Check from position 8
print(url.startswith("example", 8))
# True