Python String find() Method with Examples

In the previous article, we have discussed Python String expandtabs() Method with Examples
String find() Method in Python:

The find() method looks for the first instance of the specified value.

If the value cannot be found, the find() method returns -1.

The find() method is nearly identical to the index() method, with the exception that the index() method throws an exception if the value is not found.

Syntax:

string.find(value, start, end)

Parameters

value: This is Required. It is a string. The value to look for in the string.

start: This is optional. It is an integer. The starting point for the search. The default value is 0.

end: This is optional. It is an integer. The ending point for the search. The default value is the end of the string.

Return Value:

The find() method gives the following integer value:

  • If the value exists within the string, it returns the index of the value’s first occurrence.
  • It returns -1 if a value does not exist within the string.

Examples:

Example1:

Input:

Given string = "hello this is btechgeeks this"
Given value = "this "

Output:

The index of the given value's{ this } first occurrence =  6

Example2:

Input:

Given string = "welcome to python programs to all"
Given value = "to"
start position = 20
end position = 30

Output:

The index of the given value's{ to } first occurrence in the given start and end positions :
27

String find() Method with Examples in Python

Method #1: Using Built-in Functions (Static Input)

1)Without giving start and end positions

Approach:

  • Give the string as static input and store it in a variable.
  • Give the value as static input and store it in another variable.
  • Pass the given value as an argument to the find() function for the given string which returns the index of the given value’s first occurrence if the value exists and returns -1 if a value does not exist within the given string.
  • Store it in another variable.
  • Print the index of the given value’s first occurrence in the given string.
  • The Exit of the program.

Below is the implementation:

# Give the string as static input and store it in a variable.
gvn_str = "hello this is btechgeeks this"
# Give the value as static input and store it in another variable.
gvn_valu = "this"
# Pass the given value as an argument to the find() function for the given
# string which returns the index of the given value's first occurrence if the
# value exists and returns -1 if a value does not exist within the given string.
# Store it in another variable.
rslt = gvn_str.find(gvn_valu)
# Print the index of the given value's first occurrence in the given string.
print(
    "The index of the given value's{", gvn_valu, "} first occurrence = ", rslt)

Output:

The index of the given value's{ this } first occurrence =  6
2)With giving start and end positions

Approach:

  • Give the string as static input and store it in a variable.
  • Give the value as static input and store it in another variable.
  • Give the start position as static input and store it in another variable.
  • Give the end position as static input and store it in another variable.
  • Pass the given value, start and end positions as arguments to the find() function for the given string which returns the index of the given value’s first occurrence if the value exists and returns -1 if a value does not exist within the given string in the given start and end range.
  • Store it in another variable.
  • Print the index of the given value’s first occurrence in the given string in the given start and end positions.
  • The Exit of the program.

Below is the implementation:

# Give the string as static input and store it in a variable.
gvn_str = "welcome to python programs to all"
# Give the value as static input and store it in another variable.
gvn_valu = "to"
# Give the start position as static input and store it in another variable.
gvn_strtpositn = 20
# Give the end position as static input and store it in another variable.
gvn_endpositn = 30
# Pass the given value, start and end positions as arguments to the find()
# function for the given string which returns the index of the given value's
# first occurrence if the value exists and returns -1 if a value does not
# exist within the given string in the given start and end range.
# Store it in another variable.
rslt = gvn_str.find(gvn_valu, gvn_strtpositn, gvn_endpositn)
# Print the index of the given value's first occurrence in the given string.
print(
    "The index of the given value's{", gvn_valu, "} first occurrence in the given start and end positions :")
print(rslt)

Output:

The index of the given value's{ to } first occurrence in the given start and end positions :
27

Method #2: Using Built-in Functions (User Input)

1)Without giving start and end positions

Approach:

  • Give the string as user input using the input() function and store it in a variable.
  • Give the value as user input using the input() function and store it in another variable.
  • Pass the given value as an argument to the find() function for the given string which returns the index of the given value’s first occurrence if the value exists and returns -1 if a value does not exist within the given string.
  • Store it in another variable.
  • Print the index of the given value’s first occurrence in the given string.
  • The Exit of the program.

Below is the implementation:

# Give the string as user input using the input() function and store it in a variable.
gvn_str = input("Enter some Random String = ")
# Give the value as user input using the input() function and store it in another variable.
gvn_valu = input("Enter some Random String(value) = ")
# Pass the given value as an argument to the find() function for the given
# string which returns the index of the given value's first occurrence if the
# value exists and returns -1 if a value does not exist within the given string.
# Store it in another variable.
rslt = gvn_str.find(gvn_valu)
# Print the index of the given value's first occurrence in the given string.
print(
    "The index of the given value's{", gvn_valu, "} first occurrence = ", rslt)


Output:

Enter some Random String = good morning btechgeeks
Enter some Random String(value) = e
The index of the given value's{ e } first occurrence = 15
2)With giving start and end positions

Approach:

  • Give the string as user input using the input() function and store it in a variable.
  • Give the value as user input using the input() function and store it in another variable.
  • Give the start position as user input using the int(input()) function and store it in another variable.
  • Give the end position as user input using the int(input()) function and store it in another variable.
  • Pass the given value, start and end positions as arguments to the find() function for the given string which returns the index of the given value’s first occurrence if the value exists and returns -1 if a value does not exist within the given string in the given start and end range.
  • Store it in another variable.
  • Print the index of the given value’s first occurrence in the given string in the given start and end positions.
  • The Exit of the program.

Below is the implementation:

# Give the string as user input using the input() function and store it in a variable.
gvn_str = input("Enter some Random String = ")
# Give the value as user input using the input() function and store it in another variable.
gvn_valu = input("Enter some Random String(value) = ")
# Give the start position as user input using the int(input()) function and 
# store it in another variable.
gvn_strtpositn = int(input("Enter some random number = "))
# Give the end position as user input using the int(input()) function and 
# store it in another variable.
gvn_endpositn = int(input("Enter some random number = "))
# Pass the given value, start and end positions as arguments to the find()
# function for the given string which returns the index of the given value's
# first occurrence if the value exists and returns -1 if a value does not
# exist within the given string in the given start and end range.
# Store it in another variable.
rslt = gvn_str.find(gvn_valu, gvn_strtpositn, gvn_endpositn)
# Print the index of the given value's first occurrence in the given string.
print(
    "The index of the given value's{", gvn_valu, "} first occurrence in the given start and end positions :")
print(rslt)

Output:

Enter some Random String = good morning all
Enter some Random String(value) = o
Enter some random number = 3
Enter some random number = 10
The index of the given value's{ o } first occurrence in the given start and end positions :
6

Go through our tutorial and learn about various Python String Method Examples and learn how to apply the knowledge while dealing with strings.