Python String startswith() Method

Python String Startswith() Method

This is a detailed tutorial of the Python String startswith() method. Learn to check whether a string starts with a specified prefix (substring) or not.

Python String startswith()

The Python String method startswith() is used to check whether the string on which it is applied starts with a particular prefix or substring or not. It returns True if the string starts with the provided prefix otherwise it returns False.

Syntax

The following is the simple usage representation of the above syntax.

startswith() Parameters

The startswith() method of Python String can take three different arguments. Only the first argument is mandatory while the rest two are optional. These are defined below.

prefixString. The prefix which you want to check in the string is needed to be provided as the first argument to this method. It is the only mandatory parameter. It must be passed as a String or a Tuple containing various strings (prefixes) as items.

start and end. These are the optional parameters and it defines the starting and ending indexes of the substring to be searched for checking the prefix. These are basically the numerical indexes of the different characters in the main string. For Example, let’s say the starting index is 5. Then this method will check if the provided prefix is present at the 5th index. If it will be, it will return True, otherwise False. In other words, by providing these start and end arguments, you basically shorten the string to be checked for the prefix.

Have a look at the examples to better understand the different use cases for these parameters.

startswith() Return Value

As we make use of this function just to check if the string starts with the provided prefix or not, so it returns a boolean value, i.e. either True or False.

  • It will return True if the provided prefix is the substring with which the string starts.
  • It will return False if the string does not start with the provided prefix or substring.

Checking multiple Prefixes for a Given String using Python Tuples

Instead of passing a single prefix to check if a given string starts with it or not. You can also pass a tuple as an argument to this method that contains multiple prefixes to check if the given string starts with any of the tuple-contained prefixes.

The syntax remains almost the same, it’s just that instead of passing the first parameter as a simple string containing a single prefix, here you’ve to provide a tuple containing different string type prefixes as its items.

In case, you’ve provided a tuple as the first argument to the Python String startswith() method, it will return True if the given string starts with any of the string contained as the Tuple element and if the given string does not start with any of the string elements of the Tuple, it will return False.

Examples

The usage of the Python String startswith() method is illustrated with different examples below.

Example 1. startswith() Basic Usage without start and end arguments

The output of the above python code is shown in the following screenshot.

Python String Startswith() Method Example

The above example also illustrates that case-sensitivity matters while passing the prefix substring as an argument.

Example 2. startswith() Usage with start and end arguments

Python String Startswith() Method Example With Start And End Parameters

Example 3. Passing Tuple of Prefixes in startswith()

Python String Startswith() Function Tuple Example

  • In case, you just need to check if a particular substring is contained in a string or not, you can use the Python string find() method.

I hope you found this guide useful. If so, do share it with others who are willing to learn Python. If you have any questions related to this article, feel free to ask us in the comments section.

And do not forget to subscribe to WTMatter!

Subscribe To WTMatter!

Receive updates of our latest articles via email. Enter your email address below to get started.

Leave a Reply

Your email address will not be published. Required fields are marked *