startswith string method of Python Programming By Manish Sharma

String method “startswith” of python programming is one of the most common functions for simple search. In this tutorial I am going to show you what is this string method “startswith” and how to use it properly.

What are string methods in Python?

Methods are nothing but functions created inside a class and associated with an object. In Python, programming string methods are functions that are built into the standard “String class”. Since methods are built into standard string class thus they are available to all the string objects in Python.

Disclaimer: Though there are various string methods available in python but for now, we will stick to those that will be used most often in the upcoming tutorials. We will discuss rest of them as and when the need arises.

In order to better understand this tutorial, a working knowledge of string would be required therefore I suggest you to check out these tutorials first, single line strings and multi-line strings in Python.

String method startswith( ) of Python

As the name suggests, the method “startswith” performs a simple search on the main string and looks if it starts with the specified substring or not. If the search is successful then it “returns true” otherwise it “returns false”.

As the method “startswith” is created inside the standard string library thus it is available to all the string objects you will be creating in your Python Program.

The syntax of string method startswith of Python.

startswith (‘search-string’,  startIDX,  endIDX)

Search-string: The first argument is the search string. This is the string you want to search in the main the string. This is the mandatory parameter therefore, you must specify it in order to use the method.

  • Nature: mandatory
  • Data type: string

startIDX: The second argument is the starting index. This is an integer value which indicates the index number from where the interpreter will start the search of the sub-string. Furthermore, this is an optional argument hence you can skip it if you want to.

  • Nature: Optional
  • Datatype: Integer.

endIDX: The third argument is again an integer value which tells the index number where the interpreter stops the search. Also, this is again an optional argument.

  • Nature: Optional
  • Datatype: Integer

Optional Arguments: What happens when you skip them?

I must say, that is a very good question. Let me try to explain it to you. The starting index and the ending index combined together defines the subset of the string. That subset then becomes the search ground for the interpreter. This means that instead of searching the entire string the interpreter only searches the subset.

When you skip any of the optional argument then interpreter uses the default values for them. For example, if you skip the value for Starting Index (StartIDX) then the interpreter uses 0 as the default value for it. Which means the interpreter starts the search from the beginning of the main string. And, if you skip the value for Ending Index (EndIDX) the interpreter uses -1 as the default value for it and searches the entire main string.

What does the string method “startswith” returns?

As the output, the method “startswith” either returns “True” or returns “false”. As I mentioned earlier, if the search is successful then it “returns true” otherwise it “returns false”.

How to use the “startswith” string method in Python?

As “startswith” is built into the standard string class, therefore, we will need an object of that class to use it. That object could either be a string itself or a variable holding a string.

To call this function, we use the dot (.) notation. Like this

String_obj.startswith(‘search_str’, StartIDX, EndIDX)

Don’t be confused as all your doubts will be cleared when I’ll show you the examples in the next section.

Examples of “startswith” string method of Python

In this section, I will show you three examples of “startswith” string method which will help you understand this concept more clearly.

For the demonstration, I will be using the string “I am learning Python from RebellionRider.com” as the main string.

In the following examples I have showed you the Python Code of string method startswith. I want you to first understand those examples. Then copy those codes and run them on your machine. After that comment the output you get, in the comment section down below.

Example 1. startswith Method and The Mandatory Parameter

In the first example, I will show you, how you can call the startswith method using only the first parameter. The first parameter is the search string. It is the only parameter in the list which is mandatory. Let’s see the code

main_string = ‘I am learning Python from RebellionRider.com’     
main_string.startswith (‘Python’)

In the first statement, I created a variable and named it main_string. Also, using the assignment operator (=) I stored our string into it.

In the second statement, I called the startswith method. To call the function we first need to write the name of the variable which is holding the string. In our case, that variable is main_string followed by that we have the dot operator (.) and then the name of the function “startswith” with the parameters.

As the parameter, I only specified one which is ‘Python’. The string ‘Python’ is the search string which the interpreter will search in the main string.

As you can see, except the search string I did not specify any starting and ending index. Which means, the interpreter starts the search from the index 0. If it finds the search string there then it will return true otherwise false.  

To learn how the interpreter runs this code, I suggest you watch the video. I have explained how the execution of the code proceeds there. You can click here to enjoy that video.

Example 2: startswith Method and The Starting Index

In this example, I will show you how to call startswith method using ‘search string’ parameter and starting index (startIDX) parameter.

First of all, we will be using the same string for the demonstration. Also, the parameter starting index is optional, so if you want you can skip it. But using this parameter could help you in saving some time.

main_string = ‘I am learning Python from RebellionRider.com’     
main_string.startswith (‘Python’, 14)

Except from the added parameter 14 in the method call, everything is pretty much the same. In this code, I added the value for starting index in the function call.

When interpreter finds the value of starting index then it commences the search from that index number which here in my case is 14. Anything before that will be skipped.

Example 3: startswith with all the parameter.

Let’s say I don’t want the interpreter to search the entire string. That is a mere waste of time and expensive resources. I want it to search the string from index number 14 to 20. In this case, we can use both the optional parameters and assign these values to them. The integer value 14 will become the starting index (startIDX) and the value 20 will become the ending index (endIDX).

main_string = ‘I am learning Python from RebellionRider.com’     
main_string.startswith (‘Python’, 14, 20)

If the interpreter will find the search string ‘Python’ between these starting and ending index which is 14 and 20 of the main string then it will return true otherwise false.

String Method startswith and Common Errors.

Sometimes our carelessness could cause some errors, which could take time to solve. Let us see what some of those are.

Error 1: ‘str’ object has no attribute

starstwith string method in Python By Manish Sharma

This is basically a syntax error and usually occurs when you wrongly type the name of the function.

startswith string method of python by Manish Sharma

Always remember, in Python programming, the name of all the built-in functions are in lowercase.  

Error 2: Result is always false.

There could be many reasons for that. But most common one is typing the search string wrong. Thus, first check if you have typed your search string correctly or not. Also, as I said earlier that Python is a case sensitive language thus make sure you typed your search string in the correct case.

For example, if I replace my search string from “Python” (with capital ‘P’) with “python” (with small ‘p’) then the interpreter will never return true as there is no such word in my main string.

startswith string method of python by manish sharma

That is the tutorial on string method ‘startswith’ of Python Programming. Hope you enjoyed and learned something new. If so then please share it on your social media. Also, if you have any doubts then you can leave a comment down below or send me a message on my Facebook or on Twitter.

Thanks and have a great day.

4 COMMENTS