Python Tutorial- Find Function

Python Exercise Lists

The find() method is used to retrieve whether the string contains the target string. If it does, it returns the index of the first occurrence of the string; otherwise, it returns -1.

The syntax of the find () method is as follows:

str.find(sub[,start[,end]])

The meaning of each parameter in this format is as follows:

  • str: the original string;
  • sub: the substring to be retrieved;
  • start: indicates the starting position to start searching. If not specified, the search will start from the beginning by default;
  • end: indicates the end position of the search. If not specified, it will be searched to the end by default.

[Example] Use the find() method to retrieve the first occurrence of "e" in the string of "www.freelearningpoints.com".

string = "www.freelearningpoints.com"
string.find('e')

The output is:

6

[Example] Manually specify the position of the starting index.

string = "www.freelearningpoints.com"
string.find('e', 8)

The output is:

9

[Example] Specify the positions of the start index and end index manually.

string = "www.freelearningpoints.com"
string.find('e', 2, -3)

The output is:

6

The string between indexes (2, -3) is "w.freelearningpoints. ", and since it does not contain ".", the return value of the find () method is -1.

Note that Python also provides the rfind () method. The biggest difference from the find () method is that rfind () searches from the right side of the string. Example:

string = "www.freelearningpoints.com"
string.rfind('e')

The output is:

9

                               


More Tutorials:

Python Installation - Linux (Ubuntu)
Python Installation - Mac OS
Integrated Development Environment - IDE
Python - Basic Variables
Python - Sequence Introduction
Python - Output Formatting
Python - Escape Character
Python - Type Conversion
Python - Numbers
Python – Arithmetic Operators
Python – Assignment Operators
Python – Comparison Operators
Python – Logical Operators
Python – Precedence and Associativity Operators
Python – Bytes Type and Usage
Python – Long & Raw Strings
Python – Concatenate Function
Python – Slice Function
Python – Length and Split Function
Python – Join and Count Function
Python – Find Function
Python – Index Function
Python – Alignment Function
Python – Startswith and Endswith Function
Python – String Case Conversion
Python – Remove Specified Character
Python – Encode and Decode Function
Python – dir and help Function
Python – Input Output Introduction
Python – Basic Operation
Python – Open Function
Python – Read Function
Python – Readline Function
Python – Write Function
Python – Close Function
Python – Seek and Tell Function
Python – Pickle Module
Python - File Input Module and Linecache Module
Python - Pathlib Module
Python - Pathlib Module
Python - os.path Module
Python - fnmatch Module
Python - Tuple Introduction
Python - List Introduction
Python - List Add Element
Python - List Delete Element
Python - List Modification Element
Python - List Find Element
Python - Dictionary Introduction
Python - Dictionary Basic Operation
Python - Dictionary Method Guide
Python - Set Collection
Python - Set Basic Operation
Python - Set Basic Method
Python - Frozenset Method
Python - If Condition I
Python - If Condition II
Python - While loop
Python - For loop
Python - Pass Statement
Python - Break Statement
Python - Zip Reverse Function
Python - Function Introduction
Python - Positional Parameters
Python - Key Arguments
Python - None and Return
Python - Variable Scope
Python - Local Function
Python - Closure Method
Python - Lamdba Expression


More Python Exercises:

Python String Exercises
Python List Exercises
Python Library Exercises
Python Sets Exercises
Python Array Exercises
Python Condition Statement Exercises