How to Python Find In String


In this tutorial, we will learn about how to python find in string. The basic idea about find() function is for finding the index of character or substring from the final string.

Basic Idea of String Find( ) Method in Python.

Basically, Python find in string work as a method of finding the substring from the given string. So the indexing of the given string starts from 0 and so on. When the substring not found in the entire given string, the result gives as -1. Which means sub-string not found.

Python find in string

In Python Programming, The python find in string methods works by taking a maximum of three arguments to find the character of a substring:

  • Sub-string: – In this argument, the substring is given for which you want to search in the given string.
  • Start: – In this argument, it takes an integer from which index, the searching starts to find the sub-string in the given string.
  • End: – In this argument, It takes an integer from which index, the searching end to find the sub-string in the given string.
Syntax :      str.find(substring, start, end)

As you can see in the above syntax, the find() method takes three arguments, In which start and end are optional. But the sub-string is compulsory to find the substring from the given string. If you are not providing any optional argument, then the searching of a substring from index 0 to last index in the given string. As you can see in the example below.

Example: How to Python Find In String

Example 1:

Python Find in string program for sub-string

Code

#string
str1 = 'My Name is Technoelearn'

#Find 'is' in the given string
str1.find('is')

In the above program, The Find() method is used to finding the sub-string ‘is’ in the given string ‘My Name is Technoelearn’. So, the result gives as 8, that means the sub-string ‘is’ present on the index 8.


Example 2:

Python Find in string program for character

Code

#string
str1 = 'My Name is Technoelearn'

#Find 'is' in the given string
str1.find('t')

In the above python program, The Find() method is used to find the character ‘t’ in the given string ‘My Name is Technoelearn’. So, the result gives as -1 because, In python, the string is case sensitive that’s why it gives -1 as output that means the t does not exist.


Example 3:

Python Find in string program for character but case sensitive

Code

#string
str1 = 'My Name is Technoelearn'

#Find 'is' in the given string
str1.find('a', 5, 21)

As you can see on the above python program. The find method is used which takes a three argument which are Sub-string, Starting-Index, and Ending_Index. In this program the character ‘a’ is searching from index 5 to 21. As you can see in the result, 20 is the index of character ‘a’ which start from searching index 5 and goes to index 21.


Video Tutorial For How To Find Substring In Python

FAQ of How to Python Find In String

What is Find method in python?

In a python programming language, Find() method is used to find the substring in the given string. The searching starts at index 0 and goes to the last index.

How to Python Find In String?

In python programming, the find() method is used to find the index of character or substring from the given string. It basically takes 3 arguments substring, starting-Index, Ending-Index. syntax : str.find(substring, start, end)

What is the syntax of Find() Method in String?

In python, The Syntax of Find() method is:
Syntax : str.find(substring, starting-index, ending-index)

Learn More Python Programming Language

If you have any queries regarding Find() Method then ask on the comment section, I am happy to answer your question.

5 thoughts on “How to Python Find In String”

  1. I loved as much as you’ll receive carried out right here. The sketch
    is tasteful, your authored material stylish. nonetheless, you command get
    got an impatience over that you wish be delivering the following.
    unwell unquestionably come more formerly again since exactly
    the same nearly very often inside case you shield this increase.

    Reply

Leave a Comment

Share via
Copy link
Powered by Social Snap