×

Python String find() method

Python String find() method

The string.find() method in Python finds the first occurrence of the specified value and returns the lowest index in the string if the substring sub is found else it returns -1 if the sub is not found.

Syntax

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

Parameter

sub: This parameter represents the substring to be searched in the string str.

start( Optional): This parameter signifies the substring from where the search is to start and its default value is 0.

end: This parameter signifies the substring from where to end the search and its default value is the end of the string.

Return

This method returns the lowest index in the string if the substring sub is found else it returns -1 if the sub is not found.

Example 1

 # Python programing explaining
 # the find () method
 # initializing the string value
 strVal = "How much wood would a woodchuck chuck if a woodchuck could chuck wood?\nHe would chuck, he would, as much as he could, and chuck as much wood \nAs a woodchuck would if a woodchuck could chuck wood"
 # will search of the substring could in the string and return the index
 result1 = strVal.find('could')
 print("The index of Substring 'could' :", result1)
 result2 = strVal.find('chuck')
 print("The index of Substring 'chuck':", result2)
 # How to use find()
 if  (strVal.find('wood,') != -1):
   print("The substring 'wood' is present in the string.”)
 else:
   print("Doesn't contain the substring wood") 

Output

 The index of Substring 'could' : 53
 The index of Substring 'chuck': 26
 Doesn't contain the substring wood 

Example 2

 # Python programing explaining
 # the find () method
 # initializing the string value
 strVal = "Python is an easy language to learn."
 # will find the substring within the given start and end range.
 index = strVal.find("e", 5, 20)
 #printing the index of the substring
 print("The index value of first occurring 'e'(within range 5 to 20) is",index)
 # will return -1 if the substring if not found
 index= strVal.find("Z",3,10)
 print("The index value z is ",index) 

Output

The index value of first occurring 'e'(within range 5 to 20) is 13
The index value z is  -1

Related Topics

How to run a Python file in CMD

Introduction Today, let us learn about how to run a Python file using cmd. Cmd is nothing but command prompt. So first let us know how to run a Python code...

4 minutes read.

Palindrome program in Python

Palindrome program in python A number or string is said to be a palindrome if we invert the number or string and the string or number remains the same as the...

3 minutes read.

Scrimba python

Scrimba allows you to study whenever and wherever the topics or concepts you want. It also replaces classroom instruction with interactive screencasts, live events, and student-to-student help. Scrimba is an interactive...

3 minutes read.

Python and its advantages

What is Python? Python is an object-oriented programming language, a general-purpose programming language with a high level, and also an interpreted language that has an easy syntax and dynamic semantics. Python...

7 minutes read.

How to Concat two Dataframes in Python

Using Pandas dataframe, we can concat two dataframes or series in Python. So let's take a brief introduction to what is Pandas in Python. Pandas is a library typically used for...

7 minutes read.

Character Set in Python

A collection of legal characters that will recognize by a programming language known as a Character set. Taking python programming language as an instance. The set of characters supported by the...

6 minutes read.

Python String rfind() method

The string. rfind() method in Python returns the highest index in the string if the substring sub is found else it returns -1 if the substring is not found. Syntax string.rfind(sub [,start [,end]]) Parameter sub: This parameter...

2 minutes read.

Python Linked List

Linked List Linked lists non-contiguous linear data structure which is made up of nodes and used to store value and a pointer pointing to the next node. It is linked with...

6 minutes read.

Scraping data in python

Data scraping is a technique in which one program extracts a set of data from the output of another program. Web scraping is the most common application of this technique....

6 minutes read.

Python List

The list is one of the most versatile, mutable data-structures in Python. It can store heterogeneous data or different types of data. The list contains comma-separated values (item) within the square brackets. Creating...

4 minutes read.

Salary of Python Developers in India

In this tutorial, we will understand who python developers are and what is their salary when they work in India. Python Developers Python Developers are the people who are involved in designing...

4 minutes read.

Python String isalpha() method

Python String isalpha() method The string.isalpha () method in Python returns a boolean value true if all characters in the  string are alphabetic else for any other value it returns false. Syntax isalpha()  Parameter NA Return This...

1 minute read.

Word frequency Python

Word frequency Python In this tutorial, we will write the Python program to count the occurrence of a word (word frequency) in a given sentence. We will learn all the approaches...

5 minutes read.

How to Define a Function in Python?

What is a Function? In programming, a piece of code that executes a specific task or a group of related operations is known as a function. What does Python Functions Do? If you...

6 minutes read.

What are the Purposes of Python?

Python is a high-level programming language that is simple to use and easy to write, and Python is a beginner-friendly programming language. A beginner often prefers to start with Python...

6 minutes read.

Python Decorator

Python Decorator: A Decorator is an interesting feature of Python that helps the user design patterns and insert a new functionality to an existing object without making any modifications in...

6 minutes read.

Python Indentation

Overview The spaces at the start of a code line are known as an indentation in Python programming. Indentation is only used for readability in other programming languages like C, C++,...

8 minutes read.

Python JSON Schema

Python-jsonschema JSON: JSON stands for JavaScript Object Notation. It is a text-based format that represents structured data and can be used to interchange data among various applications. This is self-defining language...

5 minutes read.

How to create a dictionary in Python?

How to create a dictionary in python Dictionary is a data structure in Python that represents our data in the form of keys and values. Each value in a dictionary can be...

5 minutes read.

Is Python Case Sensitive

Case sensitivity is the mode of dealing with the written alphabet. The cases of the alphabet are examined and based on these words are being treated. The uppercase and lowercase...

3 minutes read.