Home » Python » Python String find() method Tutorial

About Yatin

Working as a Technical Lead in the Information Technology sector where I'm primarily involved with projects based on Java/J2EE Technologies platform (such as Spring, Spring Boot, Hibernate, Redis, etc.), novel UI technologies (Angular & React), Docker, and AWS. Recently completed the AWS Solution Architect Associate Certification.

Python String find() method Tutorial

Hello in this tutorial, we will explain the String find() method in Python programming.

1. Introduction

The find() method in python programming is used to find the occurrence of a character in a given string or a substring of a string. The method is represented by the following syntax.

string.find(search_char, start, end)

where,
search_char specifies the string to be searched
start is optional and the default value is 0
end is optional and the default value is the end of the string
Python String find

The method returns the index of the first occurrence of the given character inside the string. The method returns -1 if the value doesn’t exist inside the string. The method is different from the index() method as the latter one raises an exception if the value is not found.

1.1 Setting up Python

If someone needs to go through the Python installation on Windows, please watch this link. You can download the Python from this link.

2. Python String find() method Tutorial

I am using JetBrains PyCharm as my preferred IDE. You are free to choose the IDE of your choice. Let us dive in with the programming stuff now.

2.1 Python String find() method Tutorial

Let us understand the different use cases of the find() method in python programming.

find() method

# python string find() tutorial
# a. Returns the first occurrence of the given value
# b. If not found returns -1
# Syntax - string.find(search_char, start, end)
# where,
# start is optional and the default value is 0
# end is optional and the default value is the end of the string
string = 'Hello world! Welcome to javacodegeeks.'
def find(search_char):
    index = string.find(search_char)
    if index != -1:
        print('{} is present at index = {}'.format(search_char, index))
    else:
        print('{} is not present in string'.format(search_char))
def find_range(search_char, start, end):
    index = string.find(search_char, start, end)
    if index != -1:
        print('{} is present in the given range at index = {}'.format(search_char, index))
    else:
        print('{} is not present in string'.format(search_char))
# driver code
if __name__ == '__main__':
    find('w')
    find('y')
    find_range('e', 1, 10)
    find_range('x', 1, 5)

If everything goes well the following output will be shown in the IDE console.

Console Output

w is present at index = 6
y is not present in string
e is present in the given range at index = 1
x is not present in string

That is all for this tutorial and I hope the article served you with whatever you were looking for. Happy Learning and do not forget to share!

3. Summary

In this tutorial, we learned:

  • find() method in python programming
  • Sample program to understand the find() method use cases

You can download the source code of this tutorial from the Downloads section.

4. Download the Project

This was a tutorial about the String find() method in Python programming.

Download
You can download the full source code of this example here: Python String find() method Tutorial

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments