PYTHON

Python string.find(): Syntax, Usage, and Examples

When you're working with text in Python, you’ll often need to search for a specific substring or character. That’s where the find() method comes in. The find() function allows you to locate the position of one string inside another. It’s a simple, readable, and efficient way to search through strings without writing complex loops or regex.

The Python string.find() method returns the lowest index where the substring appears. If the substring isn’t found, it returns -1. This makes it ideal for tasks like input validation, keyword detection, or search features in your programs.

How to Use Python string.find()

The basic syntax looks like this:

string.find(substring, start, end)
  • substring (required): The text you want to find.
  • start (optional): The index where you want to begin the search.
  • end (optional): The index where the search should stop.

If you don’t specify start or end, Python searches the entire string.

Example

message = "Welcome to Python"
index = message.find("Python")
print(index)  # Output: 11

The method finds the position where "Python" starts in the original string.

When to Use the find() Method in Python

Use this method when:

  • You want to locate a character or substring
  • You're validating input or checking for matches
  • You need to extract or slice a string from a known marker
  • You want to avoid exceptions (unlike index() which raises an error if not found)

Practical Examples of string.find() in Python

Find a Word in a Sentence

text = "Python is powerful and fast."
position = text.find("powerful")
print(position)  # Output: 10

This helps you locate keywords in user input, articles, or logs.

Python Find Character in String

char = "e"
phrase = "excellent"
print(phrase.find(char))  # Output: 1

You can pass a single character just like a longer substring.

Find with Start and End Parameters

email = "support@company.com"
position = email.find("@", 5, 20)
print(position)  # Output: 7

This gives you more control over where the search begins and ends.

Handle Missing Substrings Gracefully

note = "Check your spelling."
result = note.find("grammar")
print(result)  # Output: -1

If you use .index() here, it would raise a ValueError. But find() safely returns -1.

Find Substring in String Python Style with Conditionals

subject = "Weekly Report"
if subject.find("Report") != -1:
    print("Report detected!")

This is a simple, clean way to write conditionals based on text content.

Learn More About string.find() in Python

Case Sensitivity Matters

Python’s find() method is case-sensitive by default:

msg = "Python is Fun"
print(msg.find("python"))  # Output: -1

To perform a case-insensitive search, convert both strings to lowercase:

print(msg.lower().find("python"))  # Output: 0

Count vs Find

The count() method tells you how many times a substring appears. The find() method tells you where the first one appears. For example:

sentence = "red, blue, red, green"
print(sentence.find("red"))   # Output: 0
print(sentence.count("red"))  # Output: 2

Use count() for frequency, and find() for location.

Find the Last Occurrence

If you want to locate the last occurrence of a substring, use rfind():

text = "one two one"
print(text.rfind("one"))  # Output: 8

rfind() works the same way as find(), but it searches from the right.

Use find() for Extracting Text

You can use the result of find() with slicing to extract parts of a string:

log = "Error: File not found."
index = log.find(":")
message = log[index + 2:]
print(message)  # Output: File not found.

This is useful when you know the format of the string and want to pull out specific parts.

Check for Multiple Substrings

If you need to check for several possible substrings:

query = "What is your name?"
if query.find("name") != -1 or query.find("age") != -1:
    print("Valid question")

You can use this pattern to support flexible search features or chatbot intents.

Integrate with Loops

lines = ["Start", "Middle", "End"]
for line in lines:
    if line.find("Mid") != -1:
        print("Match found:", line)

Perfect for file processing or multi-line search functions.

Related Concepts

in Keyword vs find()

You can also use in for a simpler check:

if "Python" in text:
    print("Found!")

But if you need the actual position, use find().

string.find() with User Input

command = input("Enter a command: ")
if command.find("exit") != -1:
    print("Shutting down.")

This approach lets you implement fuzzy or partial command matching.

Using string.find() in Loops to Avoid Infinite Recursion

When searching for multiple matches, update your position to avoid looping over the same location:

content = "hello hello hello"
start = 0
while True:
    pos = content.find("hello", start)
    if pos == -1:
        break
    print("Found at:", pos)
    start = pos + 1

You can use this to highlight or extract multiple instances of a word.

The Python string find method is a go-to tool when you need to locate substrings in a flexible, safe, and readable way. Whether you're building search features, validating input, or slicing content, find() provides a clean and dependable solution. By mastering String find() Python, you make your code more efficient and expressive—perfect for both beginners and experienced developers working with text data.

Learn to Code in Python for Free
Start learning now
button icon
To advance beyond this tutorial and learn Python by doing, try the interactive experience of Mimo. Whether you're starting from scratch or brushing up your coding skills, Mimo helps you take your coding journey above and beyond.

Sign up or download Mimo from the App Store or Google Play to enhance your programming skills and prepare for a career in tech.

You can code, too.

© 2025 Mimo GmbH