Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python3 startswith() Method

Python3 startswith() Method

Python3 Strings Python3 Strings


Description

The startswith() method checks whether a string begins with a specified substring, returning True if so and False otherwise. If beg and end are specified, the check is performed within that range.

Syntax

Syntax of the startswith() method:


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

Parameters

Return Value

Returns True if the string is detected, otherwise False.

Example

The following example shows how to use the startswith() function:

React example

#!/usr/bin/python3

str

=

"

this is string example....wow!!!

"

print

(

str

.

startswith

(

'

this

'

)

)

Whether the string starts with this

print

(

str

.

startswith

(

'

string

'

,

8

)

)

Whether the string starting at the ninth character starts with string

print

(

str

.

startswith

(

'

this

'

,

2

,

4

)

)

Whether the string from the 2nd character to the 4th character starts with this

The output of the above example is:


True
True
False

Python3 Strings Python3 Strings