×

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

Python String split() method

The string.split() method in Python splits a string into a list and returns a list of the words in the string. If the parameter maxsplit is given, at most maxsplit splits are done. If maxsplit is...

2 minutes read.

Difference between Python 2 and Python 3

In this tutorial, we will learn the differences between two versions of python, that is, python version 2 and python version 3. Some basic differences include- Python 2 is the older version...

3 minutes read.

Python Graph

Python Graph: In Computer Science and Mathematics, a Graph is a pictorial representation of a group of objects or elements where some elements are connected using the links. A graph...

5 minutes read.

Attributes in python

In this article, we shall learn about attributes in python. Classes are a mix of data and functions, which in reality mean attributes and methods respectively. Typically, the body of a...

3 minutes read.

Installing Packages in Python

It's critical to understand that the term "package" here refers to a collection of software that must be installed (i.e. as a synonym for a distribution). It has nothing to...

6 minutes read.

Python Dictionary values() method

Python Dictionary values() method The dictionary.values() method in Python returns a view object that displays a list of all the values in the specified dictionary. Syntax dictionary.values() Parameter NA Return This method returns a view object. The...

1 minute read.

Python Data Types

Python Data Types: The variable in Python is used to store values, and they can hold different values. Each variable in Python has its own data-type. Since Python is a...

13 minutes read.

Python Selectors

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....

4 minutes read.

API Requests using Python

What is an API? API stands for Application Programming Interface. It is commonly known as API. It provides an environment that helps two or more computer programs to contact each other....

5 minutes read.

Python round() function

Python round() function The round() function in Python returns a number rounded to ‘ndigits’ precision after the decimal point. If ndigits is omitted or is None, it returns the nearest integer to its input. Syntax round(number[, ndigits]) Parameter Number: This parameter represents the...

1 minute 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 data science course

What is meant by Data Science? When processing raw, structured, and unstructured data utilizing various technologies, algorithms, and the scientific method, data science is a detailed study of the enormous quantity...

4 minutes read.

Abstraction in Python

What is meant by abstraction generally? A very general notion of a thing or work is known as abstract. To be more precise, the process of having a brief idea but...

4 minutes read.

OSError in Python

Python: Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It is said...

4 minutes read.

Application to Search Installed Application using Tkinter in Python

Tkinter: The standard Python technique for building Graphical User Interfaces (GUIs) is Tkinter, which is included in all popular Python distributions. The only framework included in the Python standard library is...

4 minutes read.

Python Tuple count() Method

Python Tuple count() Method The tuple.count() method in Python returns the number of times a specified value appears in the tuple. Syntax tuple.count(value) Parameter value- This parameter represents the element to search in the tuple Return This...

1 minute read.

Python List count() method

Python List count() method The list.count() method returns the number of times x appears in the list. Syntax list.count(x) Parameter x: This parameter represents the value to search for and can contain any iterable (list, set, tuple, etc.) Return This method returns the...

1 minute read.

Python Prime factorization

Python Prime factorization In this tutorial, we will design a program where we will find all the prime factors of a number. Then, we will print all these prime factors of...

3 minutes read.

Python try except

Before diving right into loads of syntax we need to know what does try except is used for and how it helps users in writing programs What is Python try except? Python...

5 minutes read.

How to Call a Function in Python

How To Call a Function in Python Functions are the well-defined and structured piece of code that is used to implement specific functionality. Calling a function in python is the best...

4 minutes read.