Python String startswith() Method


Contents

Description

The startswith() method in Python checks if the string begins with str, optionally putting a restriction on the matching with the provided indices start and end. The startswith() method in Python returns a boolean. It also returns True in case the string begins with the specified prefix. It might return False when the string doesn't begin with the specified prefix.

Syntax

Here is the syntax for startswith() method:

str.startswith(str, beg=0,end=len(string));

Parameters

  • str − This refers to the string to be checked.
  • beg − This is the optional parameter in order to set start index of the matching boundary.
  • end − This is the optional parameter that end start index of the matching boundary.

Return Value

This method in Python string returns true if in case it is found matching with the string otherwise false.

Example

The following example below shows the usage of startswith() method.

#!/usr/bin/python

str = "this is string example....wow!!!";
print str.startswith( 'this' )
print str.startswith( 'is', 2, 4 )
print str.startswith( 'this', 2, 4 )

When we run above program, it produces following result:

True
True
False

Here at Intellinuts, we have created a complete Python tutorial for Beginners to get started in Python.



Navigation
Tutorial
Tutorial
Tutorial