Python string Method - startswith()
The startswith()
method is a built-in string method in Python that returns a boolean indicating whether a string starts with a specified substring.
The syntax for using startswith()
method is as follows:
string.startswith(substring, start, end)
Here, string
is the original string, substring
is the substring to search for, start
is the index to start the search (optional, default is 0), and end
is the index to end the search (optional, default is the length of the string).
The method returns True
if the string starts with the specified substring, and False
otherwise.
Here's an example of using the startswith()
method:
string = "Hello, World!" result = string.startswith("Hello") print(result)
Output:
True
In the example above, the startswith()
method was used to check whether the original string "Hello, World!"
starts with the substring "Hello"
. The resulting boolean value is stored in the variable result
, which is True
because the string does start with the specified substring.