Sunday, January 23, 2022

ItsMyCode: Python String find()

The Python String find() method is a built-in function that returns the index of the first occurrence of a substring in a given string. If not found, it returns -1.

In this article, we will learn about the Python String find() method with the help of examples.

find() Syntax

The Syntax of find() method is:

str.find(sub, start, end)

find() Parameters

The find() method can take three parameters.

  • sub – substring that needs to be searched in the given string.
  • start (optional) – starting position where the substring needs to be searched within the string
  • end (optional) – ending position where the substring needs to be searched within the string
Note: If the start and end indexes are not provided, by default, it takes 0 as the starting index and str.length-1 as the end index

find() Return Value

The find() method returns an integer value, an index of a substring.

  • If the substring is found in the given string, the find() method will return the index of the first occurrence of the substring.
  • If the substring is not found in the given string, it returns -1
Python find() vs rfind()Python String find() 4

Difference between find() method and index() method

The find() method is similar to index() method. The only major difference is that the find() method returns -1 if the substring is not found in a given string, whereas the index() method will raise the ValueError: substring not found exception.

Example 1: find() without any arguments

text = 'she sells seashells on the seashore'

# find the index of first occurence of substring 'sea'
output = text.find('sea')
print("The index of first occurrence of 'sea' is:", output)

# find the index of character 'lls'
output = text.find('lls')
print("The index of first occurrence of 'lls' is:", output)

# find the index of substring 'fish', returns -1
output = text.find('fish')
print("The index of first occurrence of 'fish' is:", output)

Output

The index of first occurrence of 'sea' is: 10
The index of first occurrence of 'lls' is: 6
The index of first occurrence of 'fish' is: -1

Example 2: find() with start and end Arguments

text = 'she sells seashells on the seashore'

# find the index of first occurence of substring 'sea'
output = text.find('sea', 25)
print("The index of first occurrence of 'sea' is:", output)

# find the index of character 'lls'
output = text.find('lls', 1, 10)
print("The index of first occurrence of 'lls' is:", output)

# find the index of substring 'fish', returns -1
output = text.find('fish', 1, 50)
print("The index of first occurrence of 'fish' is:", output)

# find the index of substring 's'
output = text.find('s', 1, -20)
print("The index of first occurrence of 's' is:", output)

Output

The index of first occurrence of 'sea' is: 27
The index of first occurrence of 'lls' is: 6
The index of first occurrence of 'fish' is: -1
The index of first occurrence of 's' is: 4


from Planet Python
via read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...