Python‎ > ‎str‎ > ‎

str.find()

http://www.adilwaseem.com/

Definition
Return with the index of the string from start value counting from 0. If not found return -1.

Syntax
x.find(sub[, start[, end]]) -> int
str.find(x)-> -> int

Parameters
x=string

Examples
The return is:
 
>>> a='adil'
>>> b='adil waseem'
>>> if b.find(a).__ge__(0):
...     print(a)
... 
adil

The request request is subject to the exception of ConnectionError in finding the turtle parameters in the www.pythonlake.com homepage.
>>> import requests
 >>> try:
...     r=requests.get('http://www.pythonlake.com',params='www.pythonlake.com')
...     t=r.text
...     t.find('turtle')
... except:
...     print(e)
...     
103986
>>> 


The returns are:
 >>> x="Python Lake"
>>> x.find("Lake")
7
>>> 
>>> "Python Lake".find("Lake")
7
>>> str.find("Python Lake", "Lake")
7

The method is used to find the word in the string.
 text1="Python Lake is ideal site for learning the Python sample codes."
print("Python found the word 'learning' at", text1.find('learning'), "characters from left to right")
>>> 
Python found the word 'learning' at 30 characters from left to right
>>> 

The returns are:
 >>> a="python Lake is a excellent sources of learning"
>>> a.find("c")
19
>>> a.find("Lake")
7
>>> a.find("le")
22
>>> 
>>> a.find("love")
-1
>>> 

Find string using "in"
The in operator is used to find the characters in string.
 >>> 
>>> "py" in "Python Lake"
False
>>> "py" in "python"
True
>>> 

class....: def....: str.find()
The function allow the user to find word in their text.
 class method_text_finder():
    def __init__(self, a,b):

        self.a=str(a)
        find1=a.find(b)
        print("The word found in your text at position:",find1+1)

        count1=a.count(b)
        print("The total word found in your text:",count1)

text=input("Please enter your text: ")
find_word=input("Please enter your text:")

method_text_finder(text,find_word)

>>> 
Please enter your text: Python Research Centre is located at Woodbridge Va
Please enter your text:Woodbridge
The word found in your text at position: 38
The total word found in your text: 1
>>> 

class....: def....: str.find()
The function allow the user to find word in their text.
 class method_text_finder():
    def __init__(self, a,b):

        self.a=str(a)
        find1=a.find(b)
        print("The word found in your text at position:",find1+1)

        count1=a.count(b)
        print("The total word found in your text:",count1)

text=input("Please enter your text: ")
find_word=input("Please enter your text:")

method_text_finder(text,find_word)

>>> 
Please enter your text: Python Research Centre is located at Woodbridge Va
Please enter your text:Woodbridge
The word found in your text at position: 38
The total word found in your text: 1
>>> 


Comments