Easy Tutorial
❮ Python Att Dictionary Get Python Os Ftruncate ❯

Python3 find() Method

Python3 Strings


Description

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

Syntax

The syntax for the find() method is:

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

Parameters

Return Value

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

Examples

The following examples demonstrate the find() method:

Example (Python 3.0+)

#!/usr/bin/python3

str1 = "tutorialpro example....wow!!!"
str2 = "exam";

print (str1.find(str2))
print (str1.find(str2, 5))
print (str1.find(str2, 10))

The output for the above example is:

7
7
-1

Example (Python 3.0+)

>>> info = 'abca'
>>> print(info.find('a'))      # Starting from index 0, find the first occurrence of the substring, returns: 0
0
>>> print(info.find('a', 1))   # Starting from index 1, find the first occurrence of the substring, returns: 3
3
>>> print(info.find('3'))      # Returns -1 if not found
-1
>>>

Python3 Strings

❮ Python Att Dictionary Get Python Os Ftruncate ❯