Python startswith() method returns True if the string starts with the specified prefix, otherwise False. Two parameters start, and the end is needed. The start is a starting index from which the index begins, and the end index is where searching stops.
The syntax of the method is: str.startswith(str, beg = 0,end = len(string));
Key Points :
• The function startwith() returns true if the string begins with the prefix we are searching for, and if it has no prefix, it returns false in this situation.
• This function’s parameter prefix is case-sensitive, indicating that Computer and computer are two distinct values.
We’ll be covering the following topics in this tutorial:
startswith() Parameters
startswith() method takes a maximum of three parameters:
• Prefix: This parameter is used to describe the prefix substring.
• start (optional): This is an optional parameter to set the matching boundary starting index.
• end (optional): It is also an optional parameter used to set the matching boundary’s ending index.
Return Value from startswith()
startswith() method returns a boolean.
• Returns True if the string begins with the prefix defined.
• Returns False if the string does not begin with the prefix defined.
Below is the python program to demonstrate the startswith() function:
str = "The Best Learning Resource for Online Education"; print (str.startswith( 'The' )) print (str.startswith( 'for', 2, 40 )) print (str.startswith( 'the', 2, 50 ))
When we run above the program, the outcome is as follows:
True
False
False
startswith() With Tuple Prefix
str = "We Love Python Programming" output = str.startswith(('We', 'Programming')) print(output) output = str.startswith(('Python', 'Programming'), 8, 25) print(output)
When we run above the program, the outcome is as follows:
True
True
Below are several other functions that we can use to work with string in Python 3