Python String Method – find()

Spread the love

find() method –

The find method returns the index of the first occurences of a substring. If not found then it returns -1.

Syntax –

string.find(sub, start, end)

sub – The sub string that you want to search in the string.

start – the start index from where the search should begin.

end – the end index before which the search should stop.

Example –

In [1]: string = "python is cool. I love python."

In [2]: string.find('python')
Out[2]: 0

In [3]: string.find('java')
Out[3]: -1

Find with start and end arguments –

In [4]: string.find('python', 10, -1)
Out[4]: 23

In [5]: string.find('java', 10, -1)
Out[5]: -1

Related Posts –

  1. Python String Method – Capitalize()
  2. Python String Method – title()
  3. Python String Method – center()
  4. Python String Method – count()
  5. Python String Method – startswith()
  6. Python String Method – endswith()

Leave a Reply