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

Python3 find() Method

Python3 find() Method

Python3 Strings Python3 Strings


Description

The find() method checks whether a string contains the substring str; if the beg (start) and end ranges are specified, it checks whether the substring is contained within that range. If the specified index is contained in the range, it returns the starting position of the index in the string; if not, it returns -1.

Syntax

Syntax of the find() method:


str.find(str, beg=0, end=len(string))

Parameters

Return Value

Returns the starting index if the substring is contained, otherwise -1.

Example

The following example demonstrates the find() method:

Example (Python 3.0+)

#!/usr/bin/python3

str1

=

"

Runoob example....wow!!!

"

str2

=

"

exam

"

;

print

(

str1

.

find

(

str2

)

)

print

(

str1

.

find

(

str2

,

5

)

)

print

(

str1

.

find

(

str2

,

10

)

)

The output of the above example is:


7
7
-1

Example (Python 3.0+)

info

=

'

abca

'

print

(

info

.

find

(

'

a

'

)

)

Starting at index 0, find the first occurrence of the substring in the string; the result is: 0

0

print

(

info

.

find

(

'

a

'

,

1

)

)

Starting at index 1, find the first occurrence of the substring in the string; the result is 3

3

print

(

info

.

find

(

'

3

'

)

)

Returns -1 if not found

1


Python3 Strings Python3 Strings