Contents
hide
In this tutorial, We will see the python string find() method with examples.
find() Method
The find() method is one of the methods in a python string. The python string find() method is used to find the position of the value.
- In the find() Method, If the value is present then its index value will be returned.
- In the find() Method, If the value is not present then it will return -1.
find() Method Parameters
Parameters | Description |
---|---|
value | The value of which position is to be found. This parameter is required. |
start | Value search where to start, This parameter is optional. |
end | Value search where to end, This parameter is optional. |
Syntax of Python string find() method: Using One Parameter
string.find(value)
Here value is a parameter. The value specifies a value of which position is to be found. This parameter is required.
Example of find() Method: Using One Parameter
#Example 1 a = "python" #string print(a.find("p")) #Output: 0
Syntax of Python string find() Method: Using 3 Parameter
string.find(value,start,end)
- Here 3 parameters are used. The value parameter is the same as the previous syntax.
- Here start is a parameter. It indicates the value search where to start. This parameter is optional.
- Here end is a parameter, It indicates the value search where to end. This parameter is also optional.
Example of find() Method: Using three Parameter
#Example 2 text = "python is a programming language" '''Searching will start from 6th indexing and end up to 20th indexing.''' print(text.find("p",6,20)) #Output: 12
Find the position of the particular word with find() Method
#Example 3 text = "python is a programming language." #find the position of the "python" word. print(text.find("python")) #return the position of the 1st letter of the word. #Output: 0
- If the particular word is available in the string, then it will return the position of the 1st letter of the word.
- If the particular word is not available in the string, then it will return -1.
Related Program
Take a sentence from the user and find the position of the first occurrence of the letter “p”.
#solution 1 text = input("Enter the sentence ") #user input print(text.find("p"))
#solution 2 text = input("Enter the sentence ") if("p" in text): print("The position of letter p is",text.find("p")) else: print("p is Not present")
Related Posts
- startswith() Method in Python
- split() Method in Python
- replace() Method in Python
- isalnum() Method in Python
- islower() Method in Python
- swapcase() Method in Python
Check your knowledge: Python Quizzes