In python, the string function find() is used find the position of a substring in a given string. In this tutorial, we’ll look its syntax and use-cases along with some examples.
Before we proceed, here’s a quick refresher on python strings – Strings are immutable sequences of unicode characters used to handle textual data. Strings implement all common sequence operations (example, iterations, membership tests, etc). For more, check out our guide on strings and other fundamentals of python.
The find() function
As mentioned above, the find()
string function gives the position of a substring within a string. It returns the lowest index in the string where the substring is found. If the substring is not found, it returns -1
. The following is the syntax:
Syntax
sample_string.find(sub, start, end)
Here, sub is the substring you want to find the position of within the sample_string.
Parameters:
- sub: The substring to find the position of.
- start (optional): The starting position from where the substring is to be checked within the string.
- end (optional): The ending position till where the substring is to be checked within the string.
Returns:
The find()
string function returns the lowest index where the substring is found within the sliced string sample_string[start:end]
. If the substring is not found, it returns -1
.
Examples
Example 1: Without optional parameters
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
# find the position of a substring
s = "to code or not to code"
sub = "code"
# use the find() function without parameters
pos = s.find("code")
# print
print("Position of '{}' in s: {}".format(sub, pos))
Output:
Position of 'code' in s: 3
In the above example, the substring 'code'
is present at two locations in the string s
. The find()
function without using the start and end optional parameters returned 3
which is the position of the first occurrence of the substring within the entire string.
Example 2: Specifying the start and end parameters in the find() function.
# find the position of a substring
s = "to code or not to code"
sub = "code"
# use the find() function
pos = s.find("code")
print("Position of '{}' in s: {}".format(sub, pos))
pos = s.find("code", 0, 4)
print("Position of '{}' in s: {}".format(sub, pos))
pos = s.find("code", 4)
print("Position of '{}' in s: {}".format(sub, pos))
pos = s.find("code", 4, 22)
print("Position of '{}' in s: {}".format(sub, pos))
pos = s.find("code", 4, 21)
print("Position of '{}' in s: {}".format(sub, pos))
Output:
Position of 'code' in s: 3
Position of 'code' in s: -1
Position of 'code' in s: 18
Position of 'code' in s: 18
Position of 'code' in s: -1
In the above example, we see the results when providing the start and end parameters to the find()
string function. To better understand the use of start and end parameters imagine them as the indices to slice a string, like s[start:end]
and then using the find() function to get the starting index of the substring.
For more, on python string functions, refer to the python docs.
To check if a substring is present in a string or not
If you just want to check whether a substring is present inside the string you can use the membership operator in
. For example:
# check if a substring is present or not in a string
s = "to code or not to code"
print("code" in s)
print("python" in s)
Output:
True
False
In the above example, the membership operator in
is used to check for the presence of substrings 'code'
and 'python'
inside the string s
.
For more on operators, refer to our guide on Python Operators.
Tutorials on python strings:
- Python – Trim Whitespace Characters from Strings
- Python – Reverse a String with Examples
- Python – Remove Character From String
- Python – Frequency of each word in String
- Python – Convert List to a String
- Python String Count – With Examples
- New String Functions in Python 3.9
- Python String Find – With Examples
- Python String Startswith – With Examples
- Python String Endswith – With Examples
- Python String Split – With Examples
- Python String Join – With Examples
- Python String Concatenation – With Examples
- Python String Replace – With Examples
- Python String Uppercase – With Examples
- Python String Lowercase – With Examples
- Python String Strip – With Examples
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.