• 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
  • VBA
  • 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 Slice a Dictionary in Python
  • 2.  Using Python to Split a Number into Digits
  • 3.  PROC FREQ Equivalent in Python
  • 4.  How to Serialize a Model Object with a List of Lists Using Django Rest Framework in Python
  • 5.  Reverse a List in Python Without Reverse Function
  • 6.  Get Name of Function in Python
  • 7.  Break Out of Function in Python with return Statement
  • 8.  pandas nsmallest – Find Smallest Values in Series or Dataframe
  • 9.  Using Python Selenium Webdriver to Refresh Page
  • 10.  Get the Count of NaN in pandas Using Python

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

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

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 © 2022 · The Programming Expert · About · Privacy Policy