Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content
No results
  • About AskPython
  • Blog
  • Contact Me
  • Free Online Developer Tools Hub
  • Home
  • Meet the Team
  • Privacy Policy
  • Python
  • Python Course for Beginners Online FREE
  • Python Tutorial – Mega Index
  • Terms and Conditions

🚀 Supercharge your YouTube channel's growth with AI.

Try YTGrowAI Free
  • Home
  • Blog
  • About AskPython
  • Python Course
  • Start Here

Python String find()

  • Vijaykrishna RamVijaykrishna Ram
  • January 25, 2020
  • Python String
Python String find()

The Python string find() method is used to check if a string is a substring of another string.

Often, when we want to check if a string contains another string, find() is very useful to solve this problem.

Let us understand how we can use this, through some simple examples.


Syntax of Python String Find()

Format:

ret = str.find(sub, start, end)

Here, find() is invoked on a string object str and returns an Integer. This checks if the substring sub lies between start and end, which are optional parameters.

NOTE: start is included, and end is not included.

find() does not take keyword arguments, so you must only mention positional arguments.

If the substring does not exist between the positions, then ret is -1. Otherwise, it is the position of the first match of the substring on the input string.

Let’s look at a simple example.

test_string = "Hello from AskPython"

# We will check if the substring
# 'AskPython' exists in test_string
print(test_string.find('AskPython', 0, len(test_string)) )

Output: 11

Since the substring exists, the position of the first character of the substring (‘A’ in ‘AskPython’) is returned, which is 11.

We can also use negative indexing to mention offsets from the end of the string. For example,

test_string = "Hello from AskPython"

# Will search from the first to the 
# second last position
print(test_string.find('AskPython', 0, -1))

# Will search from index 5 to len(str) - 1
print(test_string.find('from', 5, -1))

Output

-1
6

For the first instance, the output is negative because the find() method cannot entirely match the string “AskPython”.

The reason being that because of the -1, the find string is only searching until AskPytho, which means what we’re searching for isn’t really found.

Using Python String find() Without Start/End Arguments

We can leave the start and end arguments empty, if we wish to search across the whole string. This is the most widely used way of using Python string find() by many programmers.

We can rewrite our previous example as:

test_string = "Hello from AskPython"
print(test_string.find('AskPython'))

This will give the same output as before.

Using find() With Only start Argument

We can only specify the starting position, until the end of the string, by leaving out the end argument.

test_string = "Hello from AskPython"

# Will print 11
print(test_string.find('AskPython', 0))

# Will print -1, as the search starts
# from position 12, i.e from 'k'
print(test_string.find('AskPython', 12))

Output

11
-1

Conclusion

In this article, we have learned about the str.find() method to search for substrings.

References

  • Python Documentation for String.find()

Vijaykrishna Ram
Vijaykrishna Ram
Articles: 99
Python slice() function
Previous Post Python slice() function
Next Post Find String Length in Python
Find String Length in Python

Related Posts

What Does %s Mean in Python String Formatting?

What Does %s Mean in a Python Format String?

  • November 30, 2026
Python Convert String to List

Python Convert String to List: Choose the Right Method

  • November 29, 2026
Translate String Python

Translate String Python

  • November 29, 2026

Partner Websites

  • GoLangDocs
  • LinuxForDevices
  • CodeForGeek

Popular Categories

  • Python Programming
  • Python Modules
  • OOPS in Python
  • Python Programming Examples
  • Strings in Python

Hall of Fame

  • Python Programming
  • OOPS in Python
  • Creating Python Modules
  • Args and Kwargs in Python
  • Python Dictionary (Dict) Tutorial
  • Python List – 15 Things You MUST Know
  • Python Set – Things You MUST Know
  • Python String Functions: Methods by Task

Important Links

  • Home
  • Python Course
  • Start Here
  • Blog
  • About AskPython
Latest posts
  • np.linalg.eig: Eigenvalues and Eigenvectors
  • What Does %s Mean in a Python Format String?
  • How to Use np.sin() in NumPy
  • cv2.imread: Load Images with OpenCV
  • pd.read_json: Read JSON Into a DataFrame

Copyright © 2026 - AskPython. All rights reserved.