• Home
  • 4.24 String Searching

4.24 String Searching

View Categories

4.24 String Searching

4 min read

Searching a string means finding whether a character or substring exists within a string or determining its position.

Python provides several built-in methods for searching strings. The most commonly used methods are:

  • find()
  • rfind()
  • index()
  • rindex()

These methods help locate the position of characters or substrings. They are commonly used when validating input, processing text, parsing files, and extracting information.

The find() and rfind() methods return -1 if the substring is not found, whereas index() and rindex() raise a ValueError.


Syntax #

string.find(substring)
string.rfind(substring)
string.index(substring)
string.rindex(substring)

Components #

Component Description
string The string to search.
substring Character or sequence of characters to locate.
find() Returns the first occurrence or -1.
rfind() Returns the last occurrence or -1.
index() Returns the first occurrence or raises ValueError.
rindex() Returns the last occurrence or raises ValueError.

Example 1: Using find() #

main.py

text = "Python Programming"

print(text.find("Program"))

Output #

7

Explanation #

  • find() searches for the first occurrence of "Program".
  • The substring begins at index 7.
  • Therefore, 7 is returned.

Example 2: Searching for a Character #

main.py

word = "banana"

print(word.find("a"))

Output #

1

Explanation #

  • The first occurrence of "a" is at index 1.
  • find() always returns the first matching position.
  • Later occurrences are ignored.

Example 3: Using rfind() #

main.py

word = "banana"

print(word.rfind("a"))

Output #

5

Explanation #

  • rfind() searches from the end of the string.
  • The last "a" is located at index 5.
  • Therefore, 5 is returned.

Example 4: Substring Not Found #

main.py

text = "Python"

print(text.find("Java"))

Output #

-1

Explanation #

  • "Java" does not exist in the string.
  • find() returns -1 when no match is found.
  • No exception is raised.

Example 5: Using index() #

main.py

text = "Python Programming"

print(text.index("Program"))

Output #

7

Explanation #

  • index() behaves similarly to find() when the substring exists.
  • The substring begins at index 7.
  • The index is returned.

Example 6: User Input #

main.py

email = input("Enter your email: ")

print(email.find("@"))

Sample Input #

alice@example.com

Output #

5

Explanation #

  • The user enters an email address.
  • find() locates the position of "@".
  • The returned index can be used for validation or processing.

Example 7: Using rindex() #

main.py

text = "one two one"

print(text.rindex("one"))

Output #

8

Explanation #

  • rindex() searches from the end of the string.
  • The last occurrence of "one" begins at index 8.
  • That index is returned.

Example 8: Finding Spaces #

main.py

sentence = "Learn Python Today"

print(sentence.find(" "))

Output #

5

Explanation #

  • The first space occurs after "Learn".
  • Spaces are characters like any other.
  • Their positions can also be searched.

Example 9: Checking the Return Type #

main.py

text = "Python"

result = text.find("P")

print(type(result))

Output #

<class 'int'>

Explanation #

  • find() returns an integer.
  • The integer represents the index of the match.
  • If no match exists, -1 is returned.

Example 10: Searching for an Entire Word #

main.py

sentence = "Python is easy"

print(sentence.find("easy"))

Output #

10

Explanation #

  • find() works with complete substrings.
  • "easy" begins at index 10.
  • The index is returned.

Common Mistakes #

1. Assuming find() Raises an Error #

Incorrect

text = "Python"

print(text.find("Java"))

Incorrect Expectation #

ValueError

Actual Output #

-1

Reason

find() returns -1 when the substring is not found.


2. Assuming index() Returns -1 #

Incorrect

text = "Python"

print(text.index("Java"))

Output #

ValueError: substring not found

Reason

Unlike find(), index() raises a ValueError if the substring does not exist.


3. Confusing find() with rfind() #

Incorrect

word = "banana"

print(word.find("a"))

Incorrect Expectation #

5

Actual Output #

1

Reason

find() returns the first occurrence.
Use rfind() to obtain the last occurrence.


4. Assuming the Result Is a Boolean #

Incorrect

text = "Python"

result = text.find("P")

print(type(result))

Incorrect Expectation #

<class 'bool'>

Actual Output #

<class 'int'>

Reason

Searching methods return integer indices, not Boolean values.


Best Practices #

  • Use find() when the substring may or may not exist.
  • Use index() only when you are certain the substring is present.
  • Use rfind() and rindex() when searching from the end of the string.
  • Check whether find() returns -1 before using the index.
  • Use descriptive variable names when storing search results.

Key Points to Remember #

  • find() returns the index of the first occurrence or -1.
  • rfind() returns the index of the last occurrence or -1.
  • index() returns the first occurrence or raises ValueError.
  • rindex() returns the last occurrence or raises ValueError.
  • These methods return values of type int.
  • Searching is case-sensitive.
  • These methods do not modify the original string.

Powered by BetterDocs

Leave a Reply

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