Python string startswith() method
Python String startswith() Method
Description
The startswith() method checks whether a string begins with a given substring, optionally limiting the match to the given start and end indices.
Syntax
The syntax of the startswith() method is as follows −
var.startswith(sub, beg = 0, end = len(string));
Parameters
- sub − The string to be checked.
- beg − Optional parameter that sets the starting index of the matching boundary.
- end − Optional parameter that sets the ending index of the matching boundary.
Return Value
This method returns True if a matching string is found; otherwise, it returns False.
Example
The following example demonstrates the use of the startswith() method.
var = "Explicit is better than implicit."
var1 = var.startswith('Ex')
print ("Original string:", var)
print ("Starts with Ex:", var1)
var2 = var.startswith('b', 10, -1)
print ("Substring starting at index 10 starts with b:", var2)
When running this program, the following output is produced −
Original string: Explicit is better than implicit.
Starts with Ex: True
Substring starting at index 10 starts with b: False