startswith() in Python

Overview
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.
Syntax of startswith() in Python
The syntax of the startwith() method is given below:
Syntax: string.startswith( prefix, start, end)
Parameters of startswith() in Python
The startswith() method takes the following parameters:
- string: The string or tuple that needs to be checked for a particular string.
- start: This is an optional parameter that specifies the beginning position of the given string that needs to be checked.
- end: This is also an optional parameter that specifies the ending position where the prefix is to be checked in the given string.
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.
Scaler Placement Report and Statistics
Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.
Return Values of startswith() in Python
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.
Transform Your Career
Choose from our industry-leading programs designed for career success
Modern Software and AI Engineering Program
Master full-stack development with AI integration
+1000 moreModern Data Science and ML with specialisation in AI
Advanced data science techniques with AI specialization
+1000 moreAdvanced AIML with Specialisation in Agentic AI
Deep dive into AIML with focus on Agentic systems
+1000 moreDevOps, Cloud & AI Platform Engineering
Build and manage AI-powered cloud infrastructure
+1000 moreAI Engineering Advanced Certification by IIT-Roorkee
Premier AI engineering certification from IIT-Roorkee
Exceptions of startswith() in Python
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.
Example of startswith() in Python
Let's understand the startswith() method using an example
Output:
What is Python string startswith() Method?
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.
Use of startswith() in Python
- The startswith() method is used to check whether a given sentence starts with a particular string or not.
- The startswith() is used in text processing
- Suppose you have a list of names in a database and want to search through their first names. In that case, we can use this method
More Examples
Let's understand the startswith() method using an example.
Output:
Turn Learning into Career Growth
Example 1: startswith() Without start and end Parameters
Let's understand the startswith() method without the parameters.
Output:
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.
Output:
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.
Output:
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.
Output:
The TypeError is returned because we pass the number as a parameter in the startswith() method instead of the string datatype.
Conclusion
- The startswith() method is used to check whether a given string contains a particular prefix or not.
- The startswith() method takes two optional parameters, start, and last index.
- The startswith() method takes string and tuple of string as an argument.