Python String Find() function | Examples

Python String find function returns the lowest index of the substring if the value (substring) found in given sentences. If it’s is not found then, it returns -1 integer value. The find() function similar to the index() function,

The only difference is that the index() function throws an exception if the value is not found. In this tutorial, you will learn about find function and multiple examples.

Python String Find() function | Examples

Syntax

A simple syntax for find string in python.

Parameters

  • value – string (substring) to search  | Required
  • start – Here to start the search otherwise Default is 0 | Optional
  • end – Where to end the search. The default is to the end of the string | Optional

Return Value from find()

The find() function returns an integer value. If substring finds in a sentence, then it returns the lowest index else returns.-1

Python string find() Function Examples

It’s a simple example to find a string (pass only value)  in a sentence and print the index number in the console.

Output: Substring found at index : 0

If not found substring Example 

Another example to find a substring in a sentence, where substring not in the sentence.

Output  : -1

Where in the same situation index() function will throw an error?

find() With start and end Arguments

find a “t” in a sentence from start 5 to end 20 rang.

Output: Substring found at index: 19

Note: Python is case sensitive language, check this example, first “P” (uppercase) latter ignored.

Output : index is : 7

Do comment if you have any doubt and suggestion for this tutorial. We like to hear from you.

Note: This example (Project) is developed in PyCharm 2018.2 (Community Edition)
JRE: 1.8.0
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.13.6

Python 3.7

All Python String Find Examples are in Python 3, so it may change its different from python 2 or upgraded versions.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.