Python str.startswith() function
In Python, the str.startswith()
method is used to check whether a string starts with a specified substring. This method returns True
if the string starts with the specified substring and False
otherwise.
Syntax
- prefix: A string or a tuple of strings to check against the beginning of the string.
- start (optional): The starting index from which to check the prefix. If omitted, the check starts from the beginning of the string.
- end (optional): The ending index up to which to check the prefix. If omitted, the check goes to the end of the string.
Example Usage
- Basic usage:
- Using start and end parameters:
- Checking multiple prefixes:
Summary
- Use
str.startswith()
to determine if a string begins with a specific substring. - It can also check for multiple prefixes and allows for optional starting and ending indices to refine the check.