×

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

Dictionary to JSON Python

In python JSON (JavascriptObject Notation). In the programming language, the text file is made using the script file.We can use many built-in packages which arenamedJSON.Before using the packages, we have...

3 minutes read.

Python if statement

Python if statement Decision making is an essential feature of any programming languages. Condition checking is the strength of decision making. Python provides many decision-making statements, such as: If statementIf-else statementelif statement if statement The if statement is...

2 minutes read.

Python Kwargs Example

In this article, we'll talk about Python's kwargs notion. In Python, kwargs has two stars and passes a variable number of keyworded argument lists to the function, whereas args has...

5 minutes read.

Python File Handling

What is the file? The file is a collection of data in a single unit. It is used to store information in the non-volatile memory such as hard-disk. Computer programs generally run on the RAM...

7 minutes read.

Add a key-value pair to dictionary in Python

In programming, data type defines the type of value that a variable can hold. With help of these, we can perform various mathematical, logical, or relational operations on that particular...

5 minutes read.

Python next() function

Python next() function The next() function in Python retrieves the next item from the iterator.  Syntax next(iterator[, default]) Parameter iterable: It is a required parameter which represents an iterable object. default: This parameter represents a default value to...

1 minute read.

Face Recognition in Python

In this tutorial, we will understand what is face recognition and how it is achieved in python. Face recognition is of great utility in real-world scenarios. It is an extended step...

3 minutes read.

Front end in python

Python : Python is an object oriented programming language which is highly interpreted and is highly interactive. Python was created by Guido van Rossum in the year 1985 – 1990 .The...

4 minutes read.

Number Pattern Programs in Python

Learning Python is very easy, and it understands in better way compared to other programming languages. One of the many reasons why it has emerged as the most widely used...

4 minutes read.

Create the First GUI Application using PyQt5 in Python

GUI: A graphical user interface, or GUI, is present on most personal computers. It provides a simple experience for individuals with various computing skill levels. GUI apps may take more resources...

3 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 index() method

Python String index() method The string.index() method in Python finds the lower index of the first occurrence of the specified value or raise a ValueError if the substring is not found. Syntax index(sub[, start[, end]]) Parameter sub:...

2 minutes read.

How to Import Files in Python

Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

3 minutes read.

Python program to display ASCII value of the character

Python program to display ASCII value of the character The full form of ASCII is American Standard Code for Information Interchange. This example explains a program in Python to find the...

1 minute read.

Drop() Function in Python

Drop() Function: The python programming language consists of various libraries; some of them are pandas and matplotlib. Data scientists mainly use the pandas library to analyze the data more easily and...

3 minutes read.

Python Virtual Environment

In this tutorial, we will understand Virtual Environment in Python. We will understand the need for a virtual environment and also see how to make use of it in python....

3 minutes read.

Python Set intersection() method

Python Set intersection() method The set.intersection() method in Python returns a new set with elements that are similar between two or more sets. Syntax set.intersection(set1, set2 ... etc) Parameter set1- This parameter represents the set to search for...

2 minutes read.

PyPi TensorFlow

It is a free open-source library for high-performative numerical computations. The architecture of TensorFlow allows us for easy deployment and computing across a varied number of platforms and from desktops...

3 minutes read.

Notepad++ For Python

In this article, you are going to learn what notepad++ is and how it is integrated with python to use the python programming language. You can also learn how to...

4 minutes read.

How to declare array in python

How to declare array in python? Arrays are a great way of storing our numeric values into a variable in continuous locations. For declaring an array in C language, we used...

4 minutes read.