Skip to content

[Edit] Python: strings: .find() #7259

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
188 changes: 129 additions & 59 deletions content/python/concepts/strings/terms/find/find.md
Original file line number Diff line number Diff line change
@@ -1,101 +1,171 @@
---
Title: .find()
Description: 'Takes in a substring (and optionally start/end index), return the index number of the first occurrence of the substring inside a string.'
Title: '.find()'
Description: 'Returns the index of the first occurrence of a substring within a string.'
Subjects:
- 'Data Science'
- 'Computer Science'
- 'Web Development'
Tags:
- 'Strings'
- 'Methods'
- 'Functions'
- 'Methods'
- 'Python'
- 'Strings'
CatalogContent:
- 'learn-python-3'
- 'paths/analyze-data-with-python'
- 'paths/computer-science'
---

The `.find()` string method takes in a substring (and optionally start/end index), returns the index number of the first occurrence of the substring inside a string. It is case sensitive.
The **`.find()`** method is a built-in [string](https://www.codecademy.com/resources/docs/python/strings) method in Python that searches for a specified substring within a string and returns the index of the first occurrence. This method provides a simple and efficient way to locate text within strings, making it essential for text processing, data validation, and string manipulation tasks.

If the substring is not found, returns `-1`.
The `.find()` method performs a case-sensitive search and returns `-1` if the substring is not found, making it ideal for conditional operations where you need to check if a substring exists before performing further operations. Common use cases include parsing text files, validating user input, extracting specific information from strings, and implementing search functionality in applications.

## Syntax

```py
```pseudo
string.find(substring, start, end)
```

- `substring`: The substring to look for.
- `start` (optional): The starting index of the string to begin the search.
- `end` (optional): The starting index of the string to end the search.
**Parameters:**

- `substring`: The substring to search for within the main string
- `start` (optional): The starting index from where to begin the search (default is 0)
- `end` (optional): The ending index where the search should stop (default is the length of the string)

If `start` and `end` index are not specified, it will default to searching the substring in the whole string. So by default, `start = 0` and `end = len(string)`.
**Return value:**

## Example 1
The `.find()` method returns an integer value:

Use `.find()` without specifying the `start` and `end` index:
- Returns the index of the first occurrence of the substring if found
- Returns `-1` if the substring is not found in the string

## Example 1: Basic String Search

This example demonstrates the fundamental usage of the `.find()` method to locate a substring within a string:

```py
new_string = "I like to eat potato"
# Search for a word in a sentence
message = "Python is a powerful programming language"

# Find the index of "powerful"
index = message.find("powerful")
print(f"The word 'powerful' starts at index: {index}")

# Find a substring that doesn't exist
missing_index = message.find("Java")
print(f"The word 'Java' index: {missing_index}")

# Check if a substring exists using find()
if message.find("Python") != -1:
print("Python is mentioned in the message")
else:
print("Python is not mentioned in the message")
```

like_result = new_string.find("like")
drink_result = new_string.find("drink")
to_result = new_string.find("to")
This example results in the following output:

print(like_result) # Output: 2
print(drink_result) # Output: -1
print(to_result) # Output: 7
```shell
The word 'powerful' starts at index: 12
The word 'Java' index: -1
Python is mentioned in the message
```

- `like` is found at index 2 of the `new_string` string.
- `drink` is not found in the `new_string` string, so it's -1.
- `to` is found at two places in the `new_string` string, but `.find()` returns the first occurrence, instead of the other occurrence at index 18.
The method successfully finds "powerful" at index 12, returns -1 for the non-existent "Java", and demonstrates how to use `.find()` in conditional statements to check for substring presence.

## Example 2
## Example 2: Email Validation

Use `.find()` with only the `start` index specified:
This example shows how to use `.find()` for practical email validation by checking for the presence of the "@" symbol:

```py
new_string = "I like to eat potato"
# Email validation using find() method
def validate_email(email):
# Check if @ symbol exists
at_index = email.find("@")

if at_index == -1:
return False, "Missing @ symbol"

# Check if @ is not at the beginning or end
if at_index == 0 or at_index == len(email) - 1:
return False, "@ symbol cannot be at the beginning or end"

like_2_result = new_string.find("like", 2)
like_4_result = new_string.find("like", 4)
to_5_result = new_string.find("to", 5)
to_10_result = new_string.find("to", 10)
# Check for domain part (simple check for a dot after @)
domain_part = email[at_index + 1:]
if domain_part.find(".") == -1:
return False, "Missing domain extension"

print(like_2_result) # Output: 2
print(like_4_result) # Output: -1
print(to_5_result) # Output: 7
print(to_10_result) # Output: 18
return True, "Valid email format"

# Test email validation
emails = ["[email protected]", "invalid-email", "@example.com", "user@com"]

for email in emails:
is_valid, message = validate_email(email)
print(f"{email}: {message}")
```

This example results in the following output:

```shell
[email protected]: Valid email format
invalid-email: Missing @ symbol
@example.com: @ symbol cannot be at the beginning or end
user@com: Missing domain extension
```

- If index starts on 2 ("l" in "like"), `like` is found at index 2.
- If index starts on 4 ("k" in "like"), `like` is not found, so it's -1.
- If index starts on 5 ("e" in "like"), `to` is found at two places in the `new_string` string, but `.find()` returns the first occurrence, instead of the other occurrence at index 18.
- If index starts on 10 ("e" in "eat"), `to` is found at index 18.
This demonstrates how `.find()` can be used for input validation by checking the position and presence of specific characters in user-provided data.

## Codebyte Example
## Codebyte Example: Log File Analysis

Use `.find()` with the `start` and `end` index specified:
This example illustrates using `.find()` with optional parameters to analyze log entries and extract specific information:

```codebyte/python
new_string = "I like to eat potato"
# Log file analysis using find() with start and end parameters
log_entries = [
"2024-01-15 ERROR: Database connection failed",
"2024-01-15 INFO: User login successful",
"2024-01-15 WARNING: High memory usage detected",
"2024-01-15 ERROR: File not found exception"
]

def extract_log_info(log_entry):
# Find the log level (after the date)
date_end = log_entry.find(" ", 10) # Find space after date

if date_end != -1:
# Search for log level starting after the date
level_start = date_end + 1
colon_index = log_entry.find(":", level_start)

if colon_index != -1:
log_level = log_entry[level_start:colon_index]
message = log_entry[colon_index + 2:] # Skip ": "
return log_level, message

return "UNKNOWN", log_entry

# Process log entries
error_count = 0
for entry in log_entries:
level, message = extract_log_info(entry)
print(f"Level: {level}, Message: {message}")

if level == "ERROR":
error_count += 1

print(f"\nTotal errors found: {error_count}")
```

like_2_length_result = new_string.find("like", 2, len(new_string))
like_4_length_result = new_string.find("like", 4, len(new_string))
like_0_3_result = new_string.find("like", 0, 3)
like_0_15_result = new_string.find("like", 0, 15)
like_0_last3_result = new_string.find("like", 0, -3)
This example shows how `.find()` can be combined with string slicing to parse structured text data and extract meaningful information for further processing.

print(like_2_length_result)
print(like_4_length_result)
print(like_0_3_result)
print(like_0_15_result)
print(like_0_last3_result)
## Frequently Asked Questions

print("")
### 1. What's the difference between `.find()` and `.index()` methods?

to_0_length_result = new_string.find("to", 0, len(new_string))
to_15_length_result = new_string.find("to", 15, len(new_string))
Both methods locate substrings, but `.find()` returns -1 when the substring is not found, while `.index()` raises a ValueError exception. Use `.find()` when you want to handle missing substrings gracefully.

print(to_0_length_result)
print(to_15_length_result)
```
### 2. Is the `.find()` method case-sensitive?

Yes, `.find()` is case-sensitive. "Python" and "python" are treated as different strings. Use `.lower()` or `.upper()` on both strings for case-insensitive searches.

### 3. How do I find all occurrences of a substring?

Use `.find()` in a loop, updating the start parameter each time to search beyond the previous match, or consider using regular expressions for more complex pattern matching.