AppDividend
Latest Code Tutorials

Python String Startswith Example | Python startswith() Tutorial

0

Python String startswith Example | Python startswith() Tutorial is today’s topic. The method startswith() checks whether a string starts with str, optionally restricting the matching with the given indices start and end. Python String startswith() returns true if found matching string otherwise false. The syntax of the startswith() method is following.

Python String startswith Example

See the following syntax.

str.startswith(prefix, beg=0,end=len(string));
  1. prefix − This is the string to be checked.
  2. beg − This is the optional parameter to set a start index of the matching boundary.
  3. end − This is the optional parameter to end start index of the matching limit.

Let’s see the example.

// app.py

strA = 'Hello AppDividend'
print(strA.startswith('App', 6))

In the above code, we have checked for the prefix called App which is starting from a 6th index in the strA string.

So, the output will be true.

Here, one thing to note that the prefix is case sensitive. So, if you find an app, then it will give the false output. It will only give true if the substring is App and it starts with  6th index.

The output of the above code is following.

 

Python String Startswith Example

Let’s see the following scenario.

// app.py

strA = 'Hello AppDividend'
print(strA.startswith('app', 6))

Here, we are checking for the app and not App. So the startswith() function is case sensitive; that is why It will give us the false in the output.

 

Python startswith() Tutorial

So, the startswith() method returns True if a string starts with the specified prefix(string). If not, it returns False.

Finally, Python String startswith() Example Tutorial is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.