• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Find First Occurrence in String of Character or Substring in Python

Find First Occurrence in String of Character or Substring in Python

February 10, 2022 Leave a Comment

In Python, to find the first occurrence in a string of a character or substring, the easiest way is to use the Python string find() function.

string_variable = "This is a string variable we will search."

pos_of_first_a = string_variable.find("a")
pos_of_first_w = string_variable.find("w")

print(pos_of_first_a)
print(pos_of_first_w)

#Output:
8
26

You can also use the Python string index() function, but if the function doesn’t find the character or substring, it will throw an exception.

string_variable = "This is a string variable we will search."

pos_of_first_a = string_variable.index("a")
pos_of_first_w = string_variable.index("w")

print(pos_of_first_a)
print(pos_of_first_w)

#Output:
8 
26

When working with string variables in Python, being able to search the string variables for substrings and characters is very useful. One such capability is to find the first or last occurrence of a substring in a string.

There are many great string methods in Python, and we can find the first occurrence of a character or substring using the Python string find() function.

The find() function, or reverse find function, returns the position of the first occurrence of a given string in a string.

If find() doesn’t find the given string, then it returns -1.

Below is some sample Python code to find the first occurrence in a string of another string.

string_variable = "This is a string variable we will search to try to find some other strings."

pos_of_first_a = string_variable.find("a")
pos_of_first_to = string_variable.find("to")
pos_of_first_string = string_variable.find("string")
pos_of_first_z = string_variable.find("z")

print(pos_of_first_a)
print(pos_of_first_to)
print(pos_of_first_string)
print(pos_of_first_z)

#Output:
8
41
10
-1

If you want to find the last occurrence in a string using Python, you can use the rfind() function.

Finding the First Occurrence in String Using Start and End Arguments With Python find() Function

With the Python find() function, we can pass optional “start” and “end” arguments to only search a specific substring of the string for the occurrence of a character or substring.

For example, if we only wanted to search the second 20 characters, we would pass “20” and “40” to find().

string_variable = "This is a string variable we will search to try to find some other strings."

pos_of_first_a = string_variable.find("a")
pos_of_first_a_first_10 = string_variable.find("a",20,40)

print(pos_of_first_a)
print(pos_of_first_a_first_10)

#Output:
8 
36

Finding the First Occurrence in String of Substring Using Python index() Function

Another way we can find the first occurrence of a substring or character in a string is with the Python string index() function.

The index() function is almost the same as the find() function, but if the function doesn’t find the given substring or character, index() will throw an exception.

Below are a few examples of using the index() function in Python to the first occurrence in string of another string.

string_variable = "This is a string variable we will search."

pos_of_first_a = string_variable.index("a")
pos_of_first_w = string_variable.index("w")

print(pos_of_first_a)
print(pos_of_first_w)

#Output:
8 
26

Hopefully this article has been useful for you to learn how to find the first occurrence in string of a character or substring using Python.

Other Articles You'll Also Like:

  • 1.  How to Capitalize the First Letter of Every Word in Python
  • 2.  Does Python Use Semicolons? Why Python Doesn’t Use Semicolons
  • 3.  Truncate String in Python with Slicing
  • 4.  Backwards for Loop in Python
  • 5.  Check if Word is Palindrome Using Recursion with Python
  • 6.  Calculate Sum of Dictionary Values in Python
  • 7.  Python Check if Attribute Exists in Object with hasattr() Function
  • 8.  Get Days in Month Using Python
  • 9.  Python Try Until Success with Loop or Recursion
  • 10.  Draw Circle in Python Using turtle circle() Function

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

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

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy