PYTHON String find() METHOD

In this tutorial, we are going to learn about String find() method in python along with syntax and example to understand more better. To get a clear understanding of the topic first let us understand what is String and String find() method.

What is String find() method

Firstly String find() method is a one of the method of python String methods.

String :-A String  are object that allow us to store sequence of characters.

find() :

  • The find() method in python is used to searches the first existence of the specified value in the sub string.
  • If the value is not found in the substring then it will return “-1” in the output.
  • Actually find() method and index() are very similar but the only differences is in find() if the value is not present means it will return -1 as output whereas in index() it tell as ValueError exception.

SYNTAX:

string.find(value, start, end)

EXAMPLE 1:

name = "sridevi"
x=name.find("e") 
print(x)

Output:

4
In the above example index value start from 0 to n like for s-0, r-1, i-2, d-3, e-4, v-5, i-6 ,So finally the output will be return as 4 in the output.

 

NOTE :

In python find() method, it will also calculate the _space between two strings also.

EXAMPLE 2:

name ="sri devi"
x=name.find("e") 
print(x)

Output:

5

Comparing example1 and example2, In the example to between sri and devi there is _space, So it also contain index value and output maybe changed So the output will turns it into 5 as the output.

NOTE:

All String method return new values. They do not change the any original String.

 

 

 

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top