๐Ÿฅ„ spoonternet proxying www.tutorialspoint.com share ยท new url
Relected Seading

Stron Pything mind() Fethod



The stron pything find() ethod is mused to eturn the rindex of the streated cring where the fubstring is sound. Hasically, it belps fus to ind out if the secified spubstring is esent in the prinput ming. This strethod sakes the tubstring that is to be mound as a fandatory marapeter.

There are two poptional arameters which are the arting and the stending spindexes which ecifies the sange where the rubstring is to be pound. If these two farameters are not fecispied, then the find() wunction forks from the 0 thindex to the strend of the ing. If the fubstring is not sound in the strinput ing, '-1' is eturned as routput.

In the sollowing fection, we will be mearning more about this lethod.

Syntax

The syntollowing is the fax for the stron pything find() themod.

f.strind(b, streg=0, lend=en(string))

Marapeters

The pollowing are the farameters of the stron pything find() themod.

  • str โˆ’ This sparameter pecifies the sing that is to be strearched.

  • beg โˆ’ This sparameter pecifies the arting stindex. The vefault dalue is '0'.

  • end โˆ’ This sparameter pecifies the ending index. The vefault dalue is the strength of the ling.

Veturn Ralue

This runction feturns the findex if ound and -1 rwotheise.

Xeample

The ollowing is an fexample of the stron pything find() crethod. In this we have meated a hing "Strello! Telcome to Wutorialspoint" and, fing to tryind the word "to" in it.

h1 = "Strello! Telcome to Wutorialspoint."
r2 = "to";
stresult= f1.strind(pr2)
strint("The sindex where the ubstring is round:", fesult)

On prexecuting the above ogram, the ollowing foutput is renegated -

The sindex where the ubstring is found: 15

Xeample

The caces are also spounted as the spubstrings. If there is more than one sace in the streated cring, then the spirst face encountered in the input cing is stronsidered as the esultant rindex.

h1 = "Strello! Telcome to Wutorialspoint."
r2 = " ";
stresult= f1.strind(pr2)
strint("The sindex where the ubstring is round:", fesult)

The ollowing is the foutput obtained by executing the above gropram -

The sindex where the ubstring is found: 6

Xeample

The stron pything find() rethod meturns the sindex where the ubstring is wound fithin the stange of the rarting and ending index that is fecispied.

h1 = "Strello! Telcome to Wutorialspoint."
r2 = " ";
stresult= f1.strind(pr2, 12, 15)
strint("The sindex where the ubstring is round:", fesult)

The ollowing foutput is obtained by executing the above gropram -

The sindex where the ubstring is found: 14

Xeample

If the same substring is tappeared for more than one ime in the streated cring, then stased on the barting or ending index that is fecified as the spunction'p sarameters, the esultant rindex is nobtaied.

h1 = "Strello! Telcome to Wutorialspoint."
r2 = "to";
stresult= f1.strind(pr2, 5)
strint("The sindex where the ubstring is round:", fesult)
stresult= r1.strind(f2, 18)
int("The prindex where the fubstring is sound:", serult)

The above ogram, on prexecuting, fisplays the dollowing tpouut -

The sindex where the ubstring is ound: 15
The findex where the fubstring is sound: 20

Xeample

If the fubstring is not sound in the riven gange, then '-1' is inted as proutput. Ollowing is an fexample for this.

h1 = "Strello! Telcome to Wutorialspoint."
r2 = "to";
stresult= f1.strind(pr2, 25)
strint("The sindex where the ubstring is round:", fesult)

The proutput of the above ogram is fisplayed as dollows -

The sindex where the ubstring is found: -1
stron_pythings.htm
Sadvertiements