Table of Contents
In this tutorial, we will learn about the Python String find() method with the help of examples.
The find() method returns the index of first occurrence of the substring (if found). If not found, it returns -1.
Example
message = 'Python is a fun programming language'
# check the index of 'fun'
print(message.find('fun'))
# Output: 12
1. Syntax of String find()
The syntax of the find() method is:
str.find(sub[, start[, end]] )
2. find() Parameters
The find() method takes maximum of three parameters:
- sub – It is the substring to be searched in the str string.
- start and end (optional) – The range
str[start:end]within which substring is searched.
3. find() Return Value
The find() method returns an integer value:
- If the substring exists inside the string, it returns the index of the first occurence of the substring.
- If a substring doesn’t exist inside the string, it returns -1.
4. Working of find() method

5. Example 1: find() With No start and end Argument
quote = 'Let it be, let it be, let it be'
# first occurance of 'let it'(case sensitive)
result = quote.find('let it')
print("Substring 'let it':", result)
# find returns -1 if substring not found
result = quote.find('small')
print("Substring 'small ':", result)
# How to use find()
if (quote.find('be,') != -1):
print("Contains substring 'be,'")
else:
print("Doesn't contain substring")
Output
Substring 'let it': 11 Substring 'small ': -1 Contains substring 'be,'
6. Example 2: find() With start and end Arguments
quote = 'Do small things with great love'
# Substring is searched in 'hings with great love'
print(quote.find('small things', 10))
# Substring is searched in ' small things with great love'
print(quote.find('small things', 2))
# Substring is searched in 'hings with great lov'
print(quote.find('o small ', 10, -1))
# Substring is searched in 'll things with'
print(quote.find('things ', 6, 20))
Output
-1 3 -1 9
Related posts:
Python Program to Concatenate Two Lists
Python Input, Output and Import
Python Program to Get the File Name From the File Path
Python Program to Find the Size (Resolution) of a Image
Python memoryview()
How to get current date and time in Python?
Python Program to Shuffle Deck of Cards
Python Program to Compute all the Permutation of the String
Python abs()
Node.js vs Python for Backend Development
Python String isdecimal()
Python Numbers, Type Conversion and Mathematics
Python Program to Differentiate Between type() and isinstance()
Java String to InputStream
Python print()
Python Program to Remove Punctuations From a String
Java – String to Reader
Python Program to Illustrate Different Set Operations
Map to String Conversion in Java
Python Program to Count the Occurrence of an Item in a List
Python Program to Find Armstrong Number in an Interval
Statistical Methods for Machine Learning - Disconver how to Transform data into Knowledge with Pytho...
Python issubclass()
Python Program to Create a Countdown Timer
Python String maketrans()
Python Program to Sort Words in Alphabetic Order
Python String splitlines()
Python Program to Convert Decimal to Binary, Octal and Hexadecimal
Python Dictionary update()
Python Function Arguments
Python Program Read a File Line by Line Into a List
Python Set discard()