The startswith() method of the”str” class is used to check whether the String is starting from a given String or not. If yes, startwith() method returns true otherwise, false.
The syntax of the startwith() method is given below:
The value of the start and end index must be in the range of [0-length-1] of the String. And also, if we don’t provide the start and the end parameter by default, the starswith() method sets the start to 0 and the end value to be length-1.
The startswith() method takes the following parameters:
Return Type : bool The startswith() method returns a boolean value, i.e., True or false. The True boolean value is returned only when the given String contains the given prefix.
The typeError exception generally occurs in the startswith() function when we pass an argument other than the String. The example of the typeError exception is discussed in the example section.
# Checking whether the string is Starting from “Scaler” or not
Let’s understand the startswith() method using an example
The starswith() method is used to check whether the String is starting from the given prefix or not. We can also set the position of the prefix to be searched in the given string using the start and end parameters.
Tóm Tắt
More Examples
Let’s understand the startswith() method using an example.
##startswith() method with start parameter
string1=
"Helloo Academy"
##7 is the starting index of the Academy
(string1.startswith(
"Academy"
,
7
))
##starswith() method with the start and end parameter
##Starting index is 7.
##Ending index that is not included is 10
string2=
"Helloo Academy"
(string2.startswith(
"Aca"
,
7
,
10
))
Output:
True
Example 1: startswith() Without start and end Parameters
Let’s understand the startswith() method without the parameters.
string=
"Helloo Academy"
##Checking whether the String is Starting from "Scaler or not"
(string.startswith(
"Hello"
))
Output:
True
The true is returned because the given string starts from the “Hello” string.
Example 2: startswith() With start and end Parameters
Let’s understand how to use startswith() method using the start and the end parameters.
##starswith() method with the start and end parameter
##Starting index is 7.
##Ending index that is not included is 10
string2=
"Helloo Academy"
(string2.startswith(
"Acad"
,
7
,
10
))
##startswith() method with start parameter
string1=
"Scaler Academy"
##7 is the starting index of the Academy
(string1.startswith(
"Academy"
,
7
))
##starswith() method with the start and end parameter
##Starting index is 7.
##Ending index that is not included is 10
string2=
"Helloo Academy"
(string2.startswith(
"Aca"
,
7
,
10
))
##Ending index is greater than the length of the string and
## Starting index is negative
string3=
"Hello"
(string3.startswith(
"hello"
, -
5
,
10
))
Output:
False
True
True
False
In the first case, we get-
Example 3: startswith() With Tuple Prefix
We can also pass a tuple of the String as a parameter that will check if any prefix of the string is contained in the tuple or not.
In the below example, we have a string that contains spaces. At first, we pass a tuple of the string as a parameter in the startswith() method , and check whether the tuple contains the starting string of the given string.
string=
"This is the Helloo Academy"
##Passing a tuple of the String to check the prefix
(string.startswith((
"Hello"
,
"This"
,
"Helloo"
)));
string1=
"This is the Scaler Academy"
##Tuple is not containing any prefix
(string.startswith((
"Hello"
,
"The"
,
"Helloo1"
)));
Output:
True
False
In the first case, we get the True boolean value because the “This” string is present in the given string, but in the second case, the tuple doesn’t contain any present in the given string.
Example 4: typeError Exception
Let’s understand how typeError occurs in the startswith() method.
In the below example, we pass the number as an argument in the incorrect startswith () method.
number=
123
;
(
str
.startswith(number))
Output:
TypeError: descriptor
'startswith'
for
'str'
objects doesn
't apply to a '
int
' object`
The TypeError is returned because we pass the number as a parameter in the startswith() method instead of the string datatype.