Python find() function is used to return the lowest index value of the first occurrence of the substring from the input string; else it returns -1. The Python find() is an in-built string method that returns the index position of the character if found; else it returns value -1.
There might arise a situation where we would want to find out the first occurrence of a specific element from a collection of data. To carry out this operation, Python provides us with a string handling function. Python find() is used in such cases. In this article, we will learn about Python find() method with the help of various programming examples.
Python find Syntax
Python find() function follows the below-mentioned syntax:
string.find(substr, start, end)
Note – The find() function is case-sensitive.
Python string find Parameters
Python find() string method accepts 3 parameters:
- substr – the substring to be searched for in the given string
- start (Optional) – starting position from where the substr is to be searched. The default value is 0.
- end (Optional) – ending position till where the substr is to be searched. The default value is the end of string or length of the string.
Return value from Python list find
The Python find() method returns an integer value:
- It returns an index position of the first occurrence of the substring, only if the substring exists in the input string.
- If the substring is not found, then the function return value -1.
Example 1: Python find With No start and end Argument
The find() method will look for the substring and return the place of the substring’s first occurrence. If the substring appears numerous times in the given string, it will still return the index or position of the first occurrence.
Example
# Python program to illustrate find()
txt = 'Today we will learn about Finance and Accounting'
# first occurrence of 'Today'(case sensitive)
result = txt.find('Today')
print("Index of Substring 'Today':", result)
# first occurrence of 'Finance'
result = txt.find('Finance')
print("Index of Substring 'Finance':", result)
# first occurrence of 'learn'
result = txt.find('learn')
print("Index of Substring 'learn':", result)
# character does not exist
result = txt.find('Marketing')
print("Index of Substring 'Marketing':", result)
Output
Index of Substring 'Today': 0
Index of Substring 'Finance': 26
Index of Substring 'learn': 14
Index of Substring 'Marketing': -1
Example 2: find() With start and end Arguments
# Python program to illustrate find()
txt = 'I am currently pursuing MBA in Finance'
# start search from index position 25
print(txt.find('pursuing', 25))
# start and end search from index position 5 to 30
print(txt.find('MBA', 5, 30))
# start and end search from index position 30 to 40
print(txt.find('in', 30, 40))
Output
-1
24
32
Difference between find() and rfind()
The Python function rfind() is similar to the find() function, with the sole difference being that rfind() returns the highest index (from the right) for the provided substring, whereas find() returns the index position of the element’s first occurrence, i.e. the very first index. If the substring is not found, both rfind() and find() will return -1.
Example
txt = 'Will you text? Or should I text? Is it okay to text?'
print("The position of 'text' using find():", txt.find('text'))
print("The position of 'text' using rfind():", txt.rfind('text'))
print("The position of 'i' using find():", txt.find('i'))
print("The position of 'i' using rfind():", txt.rfind('i'))
Output
The position of 'text' using find(): 9
The position of 'text' using rfind(): 47
The position of 'i' using find(): 1
The position of 'i' using rfind(): 36
FAQs on Python find
Q1. What is find() in Python? How do you use find() in Python?
There might arise a situation where we would want to find out the first occurrence of a specific element from a collection of data. Python find() is used in such cases. Python find() function is used to return the lowest index value of the first occurrence of the substring from the input string; else it returns -1.
Python find() function follows the below-mentioned syntax:
string.find(substr, start, end)
Example
txt = 'Sale Sale Sale - TV Laptop Mobile - Sale Sale Sale'
print("Index Position of 'Laptop' is:", txt.find('Laptop'))
print("Index Position of 'Sale' is:", txt.find('Sale', 15))
print("Index Position of 'Sale' is:", txt.find('Sale', 3, 10))
print("Index Position of 'Sale' is:", txt.find('Sale', 40, 50))
Output
Index Position of 'Laptop' is: 20
Index Position of 'Sale' is: 36
Index Position of 'Sale' is: 5
Index Position of 'Sale' is: 41
Q2. What is property in Python?
The property() function is used in Python classes to define properties. In Python, the property() function offers an interface to instance attributes. It, like Java and C#, encapsulates instance characteristics and gives a property. The property() method takes as inputs the get, set, and delete methods and returns an object of the property class. The syntax is as follows:
property(fget, fset, fdel, doc)
The Python property() function accepts 4 optional parameters. They are:
fget (optional) = function for getting an attribute value. Default value is None
fset (optional) = function to set an attribute value. Default value is None
fdel (optional) = function used to delete an attribute value. Default value is None
doc (optional) = string that contains documentation (a docstring) for the attribute. Defaults to None
Q3. What are class properties in Python?
The property() function in Python can be used to define a class property. The following example shows how to use the property() function to create a property in a Class.
Example
# Python program to illustrate property() function
class Person:
   def __init__(self, name):
       self._name = name
   # getter function
   def get_name(self):
       print('Getting name...')
       return self._name
   # setter function
   def set_name(self, value):
       print('Setting name to:', value)
       self._name = value
   # deleter function
   def del_name(self):
       print('Deleting name...')
       del self._name
   # Set property() to use get_name, set_name and del_name methods
   name = property(get_name, set_name, del_name)
p = Person('David')
print(p.name)
p.name = 'Rocky'
del p.name
Output
Getting name...
David
Setting name to: Rocky
Deleting name...
In the above program, we have defined class Person. It has a single property attribute ‘name’. We have also declared 3 methods, they are get_name, set_name, and del_name.
- get_name is used to retrieve the name of the Person,
- set_name is used to set the name of the Person,
- del_name is used to delete the name of the Person.
Q4. What are methods and properties in Python?
Objects are used in Object-Oriented Programming. These objects are made up of properties and behaviors. Furthermore, attributes specify the object’s properties, whereas methods define its action. These methods are contained within a class. These methods are reusable pieces of code that can be called or invoked at any point in the program. Python methods are classified into three types: Class Method, Instance Method, Static Method
Properties, on the other hand, are specific types of attributes that have getter, setter, and delete methods such as __get__, __set__, and __delete__. Â Attributes are described by data variables such as name, age, height, and so on.
Leave a Reply