Python startswith() method explained [Easy Examples]


Written by - Bashir Alam
Reviewed by - Deepak Prasad

 

Introduction to python startswith() method

Python is well known for its various and powerful built-in functions. One of its functions, which we will cover today is the python startswith() method. Sometimes, we might need to check the starting part of any of the strings for the programming purpose. In such a case, the python startswith() method helps us to do. In this tutorial, we will learn about startswith() method in detail considering different examples.

We will also cover the different types of parameters that this method can take and how they are useful in solving real problems.  This tutorial will help you to get a deep knowledge of the python startswith() method.

 

Getting started with python string startswith() method

Before jumping into the python startswith() method, we should know about the indexing of string in Python, Because later in this tutorial we will use string indexing to locate and search a piece of string at a specific location by providing it index number. As we all know, a string is a sequence of characters such as numbers, spaces, letters and symbols. We can access different parts of strings in the same way as we do for lists. Every character in a string has an index number starting from 0 to so on. The index is simply the location of a character in the string. For example, here is one string "Bashir", the character "B" is at index 0,  "a" is at index one, and so on.

 

Syntax of python string startswith() method

The python startswith() method returns True if a string starts with another specified string, else it will return False. This method is very useful when we want to search for a specific piece of string. Here is a simple syntax of startswith() method.

string_name.startswith(sub_string/tuple, [, start [, end]])

The python string startswith() method accepts the following parameters:

  • prefix is a string or a tuple of strings to search for. The prefix parameter is mandatory.
  • start is the position that the method starts looking for the prefix. The start parameter is optional.
  • end is the position in the string that the method stops searching for the prefix. The end parameter is also optional.

Note that the python startswith() method is case-sensitive.

In other words, it will look for the prefix case-sensitively. For example if we have a string "Bashir" and if apply the python startswith() method to find if the string starts with the letter "b", we will get False, because treats upper case B as a different character and lower case b as different.

 

Examples of Python string startswith() method "without" start parameter

As we already discussed that the python startswith() takes one required and two optional parameters. In this section, we will take examples by providing only the required parameter. Let say we have different sentences and we want to know if ech of the sentences starts with a specific word or letter. We can do this by using python startswith() method. See example below:

# string one
string1 = "This is our school"

# string two
string2 = "My name is khan"

# string three
string3 = "+996-3028374"

# check string
print(string1.startswith("This"))
print(string2.startswith("my"))
print(string3.startswith("+996"))

Output:

True
False
True

Notice that we get False for the string "my" as well because there is a difference between uppercase letters and lowercase letters. If we want our program to ignore the case differences and make it case-insensitive, we can do that by applying the following different methods.

# string
string = "My name is khan"

# checking
print(string.lower().startswith("my"))

Output:

True

Notice that this time we get True and our program ignored the case difference. Actually, it didn't ignore it, we applied lower() method on our string which converts all the letters of the main string into lower case and then checks the given string. If our input string is in upper case then we have to convert our main string into uppercase as well. See the code below:

# string
string = "My name is khan"

# checking
print(string.upper().startswith("MY"))

Output:

True

This time we used upper() method to convert our main string into the upper case to ignore the case difference.

 

Examples of Python string startswith() method "with" start parameter

Now let us solve some examples by giving an optional parameter of starting string. It is actually giving an index number so that the python startswith() method will start looking for the prefix from the specified position. See the example below which takes an optional parameter.

# string 1
string1 = "My name is khan"

# string two
string2 = "this is my school"

# string three
string3 = "+996-34343435"

# checking
print(string1.startswith("name", 3))
print(string2.startswith("school", 11))
print(string3.startswith("+996", 3))

Output:

True
True
False

Notice that we get True for the first two cases because the specified position of the given string matches while we get False in the last case because the specified string is not found in the given position.

As we did previously, we can ignore the case sensitivity by using lower() and upper() methods. See the example below:

# string 1
string1 = "My name is Khan"

# checking
print(string1.startswith("khan", 11))

# ignores the case difference
print(string1.lower().startswith("khan", 11))

Output:

False
True

Notice that we get False in the first case because we didn't apply lower() method and it gives False because of case difference. While in the second one, we applied lower() method so that it first converts the main string into lower case and then checks for the given sub-string.

In the same way, we can ignore the case difference by applying upper() method, if our input string is in upper case letters. See the example below:

# string 1
string1 = "My name is Khan"

# checking
print(string1.startswith("KHAN", 11))

# ignores the case difference
print(string1.upper().startswith("KHAN", 11))

Output:

False
True

Notice that we get True for the case where we applied upper() method.

 

Examples of python startswith() method with ending parameter

So far we have learned how to use python startswith() method and how the starting parameter works. In this section, we will learn about the python startswith() method by giving one more optional parameter and we will see how it works. The second optional parameter that this method takes is the ending position of the sub-string. See the following example to get yourself familiar with the ending parameter in startswith() method.

# string 1
string1 = "My name is Khan"

# string two
string2 = "this is my school"

# string three
string3 = "+996-3746383"

# checking
print(string1.startswith("name", 3, 11))
print(string2.startswith("is", 3, 14))
print(string3.startswith("+9", 0, 12))

Output:

True
False
True

Notice that we get True for the first and last one and get False for the second one. Python startswith() method will take the two optional parameters and checks the availability of sub-strings in the specified potions ( starting and ending).

We can also apply the lower() and upper() method to ignore the case differences. See the example below:

# string 1
string1 = "My name is Khan"

# checking
print(string1.startswith("Name", 3, 11))
print(string1.upper().startswith("NAME", 3, 11))
print(string1.lower().startswith("name", 3, 11))

Output:

False
True
True

Notice that we get False in the first one because python startswith() method is case sensitive.

 

Use python startswith() method with tuple

So far we have learned how we can apply the python startswith() method on strings by taking different examples. Now in this section, we will see how we can use a tuple to search a sub-string. This method is useful when we want to know if any of the multiple string is present in our main string or not. See the example below which takes a tuple of strings and searches using python startswith() method.

# string 1
string1 = "My name is Khan"

# string two
string2 = "this is my school"

# checking using tuple
print(string1.startswith(("My", "this", "We")))
print(string2.startswith(("We","they", "This")))

Output:

True
False

Here the python startswith() methods, will take each element from the tuple and searches in the given string, if the substring is found, it will return True, else will return False. In the above example notice that it returns True for the first case because the sub-string "My" was in our main string while it returns False for the second case because non of the sub-strings were found in our main string.

We can also use lower() and upper() methods to ignore the case differences and can also use the optional two parameters ( starting and ending) with tuple as well. See the following example.

# string 1
string1 = "My name is Khan"

# string two
string2 = "this is my school"

# checking using tuple
print(string1.startswith(("My", "this", "We"), 0, 10))
print(string2.upper().startswith(("We","they", "THIS"), 0, 10))

Output:

True
True

 

Summary

The  Python startswith() method returns True if the string starts with the specified value, otherwise False. This method is very useful we need to search for a substring in a text. It can also take an optional two parameters to specify the starting and ending positions of the substring. If we will not specify the starting position of the substring, then by default, it will check at index 0.  In this tutorial, we learned about python startswith() method by taking different examples. We discussed about the optional parameters and learned how they actually work. Moreover, we also learned how to give a tuple of strings to search in a text. All in all, this tutorial covers everything about python startswith() method.

 

Further Reading

python endwith method
documentation of startswith method

 

Bashir Alam

He is a Computer Science graduate from the University of Central Asia, currently employed as a full-time Machine Learning Engineer at uExel. His expertise lies in OCR, text extraction, data preprocessing, and predictive models. You can reach out to him on his Linkedin or check his projects on GitHub page.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment

X