The reason why you always got True
has already been given, so I’ll just offer another suggestion:
If your file is not too large, you can read it into a string, and just use that (easier and often faster than reading and checking line per line):
with open('example.txt') as f:
if 'blabla' in f.read():
print("true")
Another trick: you can alleviate the possible memory problems by using mmap.mmap()
to create a «string-like» object that uses the underlying file (instead of reading the whole file in memory):
import mmap
with open('example.txt') as f:
s = mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ)
if s.find('blabla') != -1:
print('true')
NOTE: in python 3, mmaps behave like bytearray
objects rather than strings, so the subsequence you look for with find()
has to be a bytes
object rather than a string as well, eg. s.find(b'blabla')
:
#!/usr/bin/env python3
import mmap
with open('example.txt', 'rb', 0) as file,
mmap.mmap(file.fileno(), 0, access=mmap.ACCESS_READ) as s:
if s.find(b'blabla') != -1:
print('true')
You could also use regular expressions on mmap
e.g., case-insensitive search: if re.search(br'(?i)blabla', s):
To find a word in a file using Python:
- Specify a target word.
- Open a file.
- Loop through the file line by line.
- Check if any line has the target word. If it does, print the line number and end the loop.
For example, let’s check where (if any) the word “test” is in a file called “example.txt”.
word = "test" with open("example.txt", "r") as file: for line_number, line in enumerate(file, start=1): if word in line: print(f"Word '{word}' found on line {line_number}") break print("Search completed.")
In my case, the word is found in the second line. So the output is:
Word 'test' found on line 2 Search completed.
(The enumerate(file, start=1)
matches each line with an index. Line 1 has an index of 1, line 2 has an index of 2, and so on. This simplifies the loop. Check out this article to learn more about enumerating in Python.)
How to Find the Longest Word in a File Using Python
To find the longest word(s) in a file:
- Open a file.
- Store the words in memory.
- Find the longest word.
- Find other possible words that are equally long.
For example, let’s find out the longest words in a file called “example.txt”:
with open("example.txt", "r") as file: words = file.read().split() longest_word = max(words, key=len) longest_words = [word for word in words if len(word) == len(longest_word)] print(longest_words)
If you are confused, the max()
function:
- Loops through the words
- Applies
len
function on each word. - Returns the word with the greatest value returned by the
len
. In other words, the longest word.
Also, learn more about list comprehensions to create shorthand for loops like the one in the last line.
How to Find and Replace a Word in a File Using Python
To find and replace a word in a file with Python:
- Open a file.
- Read the file in memory.
- Find and replace specific words.
- Write the fixed-up data back to the file.
Here is an example:
# Read the file in memory with open("example.txt", "r") as file: data = file.read() # Replace matches data = data.replace("test", "banana") # Write the data back to the file with open("example.txt", "w") as file: file.write(data)
Thanks for reading. Happy coding!
Further Reading
- How to Find All Files With Extension
- How to Write to a File in Python
- The with Statement in Python
About the Author
-
I’m an entrepreneur and a blogger from Finland. My goal is to make coding and tech easier for you with comprehensive guides and reviews.
Recent Posts
Watch Now This tutorial has a related video course created by the Real Python team. Watch it together with the written tutorial to deepen your understanding: Check if a Python String Contains a Substring
If you’re new to programming or come from a programming language other than Python, you may be looking for the best way to check whether a string contains another string in Python.
Identifying such substrings comes in handy when you’re working with text content from a file or after you’ve received user input. You may want to perform different actions in your program depending on whether a substring is present or not.
In this tutorial, you’ll focus on the most Pythonic way to tackle this task, using the membership operator in
. Additionally, you’ll learn how to identify the right string methods for related, but different, use cases.
Finally, you’ll also learn how to find substrings in pandas columns. This is helpful if you need to search through data from a CSV file. You could use the approach that you’ll learn in the next section, but if you’re working with tabular data, it’s best to load the data into a pandas DataFrame and search for substrings in pandas.
How to Confirm That a Python String Contains Another String
If you need to check whether a string contains a substring, use Python’s membership operator in
. In Python, this is the recommended way to confirm the existence of a substring in a string:
>>>
>>> raw_file_content = """Hi there and welcome.
... This is a special hidden file with a SECRET secret.
... I don't want to tell you The Secret,
... but I do want to secretly tell you that I have one."""
>>> "secret" in raw_file_content
True
The in
membership operator gives you a quick and readable way to check whether a substring is present in a string. You may notice that the line of code almost reads like English.
When you use in
, the expression returns a Boolean value:
True
if Python found the substringFalse
if Python didn’t find the substring
You can use this intuitive syntax in conditional statements to make decisions in your code:
>>>
>>> if "secret" in raw_file_content:
... print("Found!")
...
Found!
In this code snippet, you use the membership operator to check whether "secret"
is a substring of raw_file_content
. If it is, then you’ll print a message to the terminal. Any indented code will only execute if the Python string that you’re checking contains the substring that you provide.
The membership operator in
is your best friend if you just need to check whether a Python string contains a substring.
However, what if you want to know more about the substring? If you read through the text stored in raw_file_content
, then you’ll notice that the substring occurs more than once, and even in different variations!
Which of these occurrences did Python find? Does capitalization make a difference? How often does the substring show up in the text? And what’s the location of these substrings? If you need the answer to any of these questions, then keep on reading.
Generalize Your Check by Removing Case Sensitivity
Python strings are case sensitive. If the substring that you provide uses different capitalization than the same word in your text, then Python won’t find it. For example, if you check for the lowercase word "secret"
on a title-case version of the original text, the membership operator check returns False
:
>>>
>>> title_cased_file_content = """Hi There And Welcome.
... This Is A Special Hidden File With A Secret Secret.
... I Don't Want To Tell You The Secret,
... But I Do Want To Secretly Tell You That I Have One."""
>>> "secret" in title_cased_file_content
False
Despite the fact that the word secret appears multiple times in the title-case text title_cased_file_content
, it never shows up in all lowercase. That’s why the check that you perform with the membership operator returns False
. Python can’t find the all-lowercase string "secret"
in the provided text.
Humans have a different approach to language than computers do. This is why you’ll often want to disregard capitalization when you check whether a string contains a substring in Python.
You can generalize your substring check by converting the whole input text to lowercase:
>>>
>>> file_content = title_cased_file_content.lower()
>>> print(file_content)
hi there and welcome.
this is a special hidden file with a secret secret.
i don't want to tell you the secret,
but i do want to secretly tell you that i have one.
>>> "secret" in file_content
True
Converting your input text to lowercase is a common way to account for the fact that humans think of words that only differ in capitalization as the same word, while computers don’t.
Now that you’ve converted the string to lowercase to avoid unintended issues stemming from case sensitivity, it’s time to dig further and learn more about the substring.
Learn More About the Substring
The membership operator in
is a great way to descriptively check whether there’s a substring in a string, but it doesn’t give you any more information than that. It’s perfect for conditional checks—but what if you need to know more about the substrings?
Python provides many additonal string methods that allow you to check how many target substrings the string contains, to search for substrings according to elaborate conditions, or to locate the index of the substring in your text.
In this section, you’ll cover some additional string methods that can help you learn more about the substring.
By using in
, you confirmed that the string contains the substring. But you didn’t get any information on where the substring is located.
If you need to know where in your string the substring occurs, then you can use .index()
on the string object:
>>>
>>> file_content = """hi there and welcome.
... this is a special hidden file with a secret secret.
... i don't want to tell you the secret,
... but i do want to secretly tell you that i have one."""
>>> file_content.index("secret")
59
When you call .index()
on the string and pass it the substring as an argument, you get the index position of the first character of the first occurrence of the substring.
But what if you want to find other occurrences of the substring? The .index()
method also takes a second argument that can define at which index position to start looking. By passing specific index positions, you can therefore skip over occurrences of the substring that you’ve already identified:
>>>
>>> file_content.index("secret", 60)
66
When you pass a starting index that’s past the first occurrence of the substring, then Python searches starting from there. In this case, you get another match and not a ValueError
.
That means that the text contains the substring more than once. But how often is it in there?
You can use .count()
to get your answer quickly using descriptive and idiomatic Python code:
>>>
>>> file_content.count("secret")
4
You used .count()
on the lowercase string and passed the substring "secret"
as an argument. Python counted how often the substring appears in the string and returned the answer. The text contains the substring four times. But what do these substrings look like?
You can inspect all the substrings by splitting your text at default word borders and printing the words to your terminal using a for
loop:
>>>
>>> for word in file_content.split():
... if "secret" in word:
... print(word)
...
secret
secret.
secret,
secretly
In this example, you use .split()
to separate the text at whitespaces into strings, which Python packs into a list. Then you iterate over this list and use in
on each of these strings to see whether it contains the substring "secret"
.
Now that you can inspect all the substrings that Python identifies, you may notice that Python doesn’t care whether there are any characters after the substring "secret"
or not. It finds the word whether it’s followed by whitespace or punctuation. It even finds words such as "secretly"
.
That’s good to know, but what can you do if you want to place stricter conditions on your substring check?
Find a Substring With Conditions Using Regex
You may only want to match occurrences of your substring followed by punctuation, or identify words that contain the substring plus other letters, such as "secretly"
.
For such cases that require more involved string matching, you can use regular expressions, or regex, with Python’s re
module.
For example, if you want to find all the words that start with "secret"
but are then followed by at least one additional letter, then you can use the regex word character (w
) followed by the plus quantifier (+
):
>>>
>>> import re
>>> file_content = """hi there and welcome.
... this is a special hidden file with a secret secret.
... i don't want to tell you the secret,
... but i do want to secretly tell you that i have one."""
>>> re.search(r"secretw+", file_content)
<re.Match object; span=(128, 136), match='secretly'>
The re.search()
function returns both the substring that matched the condition as well as its start and end index positions—rather than just True
!
You can then access these attributes through methods on the Match
object, which is denoted by m
:
>>>
>>> m = re.search(r"secretw+", file_content)
>>> m.group()
'secretly'
>>> m.span()
(128, 136)
These results give you a lot of flexibility to continue working with the matched substring.
For example, you could search for only the substrings that are followed by a comma (,
) or a period (.
):
>>>
>>> re.search(r"secret[.,]", file_content)
<re.Match object; span=(66, 73), match='secret.'>
There are two potential matches in your text, but you only matched the first result fitting your query. When you use re.search()
, Python again finds only the first match. What if you wanted all the mentions of "secret"
that fit a certain condition?
To find all the matches using re
, you can work with re.findall()
:
>>>
>>> re.findall(r"secret[.,]", file_content)
['secret.', 'secret,']
By using re.findall()
, you can find all the matches of the pattern in your text. Python saves all the matches as strings in a list for you.
When you use a capturing group, you can specify which part of the match you want to keep in your list by wrapping that part in parentheses:
>>>
>>> re.findall(r"(secret)[.,]", file_content)
['secret', 'secret']
By wrapping secret in parentheses, you defined a single capturing group. The findall()
function returns a list of strings matching that capturing group, as long as there’s exactly one capturing group in the pattern. By adding the parentheses around secret, you managed to get rid of the punctuation!
Using re.findall()
with match groups is a powerful way to extract substrings from your text. But you only get a list of strings, which means that you’ve lost the index positions that you had access to when you were using re.search()
.
If you want to keep that information around, then re
can give you all the matches in an iterator:
>>>
>>> for match in re.finditer(r"(secret)[.,]", file_content):
... print(match)
...
<re.Match object; span=(66, 73), match='secret.'>
<re.Match object; span=(103, 110), match='secret,'>
When you use re.finditer()
and pass it a search pattern and your text content as arguments, you can access each Match
object that contains the substring, as well as its start and end index positions.
You may notice that the punctuation shows up in these results even though you’re still using the capturing group. That’s because the string representation of a Match
object displays the whole match rather than just the first capturing group.
But the Match
object is a powerful container of information and, like you’ve seen earlier, you can pick out just the information that you need:
>>>
>>> for match in re.finditer(r"(secret)[.,]", file_content):
... print(match.group(1))
...
secret
secret
By calling .group()
and specifying that you want the first capturing group, you picked the word secret without the punctuation from each matched substring.
You can go into much more detail with your substring matching when you use regular expressions. Instead of just checking whether a string contains another string, you can search for substrings according to elaborate conditions.
Using regular expressions with re
is a good approach if you need information about the substrings, or if you need to continue working with them after you’ve found them in the text. But what if you’re working with tabular data? For that, you’ll turn to pandas.
Find a Substring in a pandas DataFrame Column
If you work with data that doesn’t come from a plain text file or from user input, but from a CSV file or an Excel sheet, then you could use the same approach as discussed above.
However, there’s a better way to identify which cells in a column contain a substring: you’ll use pandas! In this example, you’ll work with a CSV file that contains fake company names and slogans. You can download the file below if you want to work along:
When you’re working with tabular data in Python, it’s usually best to load it into a pandas DataFrame
first:
>>>
>>> import pandas as pd
>>> companies = pd.read_csv("companies.csv")
>>> companies.shape
(1000, 2)
>>> companies.head()
company slogan
0 Kuvalis-Nolan revolutionize next-generation metrics
1 Dietrich-Champlin envisioneer bleeding-edge functionalities
2 West Inc mesh user-centric infomediaries
3 Wehner LLC utilize sticky infomediaries
4 Langworth Inc reinvent magnetic networks
In this code block, you loaded a CSV file that contains one thousand rows of fake company data into a pandas DataFrame and inspected the first five rows using .head()
.
After you’ve loaded the data into the DataFrame, you can quickly query the whole pandas column to filter for entries that contain a substring:
>>>
>>> companies[companies.slogan.str.contains("secret")]
company slogan
7 Maggio LLC target secret niches
117 Kub and Sons brand secret methodologies
654 Koss-Zulauf syndicate secret paradigms
656 Bernier-Kihn secretly synthesize back-end bandwidth
921 Ward-Shields embrace secret e-commerce
945 Williamson Group unleash secret action-items
You can use .str.contains()
on a pandas column and pass it the substring as an argument to filter for rows that contain the substring.
When you’re working with .str.contains()
and you need more complex match scenarios, you can also use regular expressions! You just need to pass a regex-compliant search pattern as the substring argument:
>>>
>>> companies[companies.slogan.str.contains(r"secretw+")]
company slogan
656 Bernier-Kihn secretly synthesize back-end bandwidth
In this code snippet, you’ve used the same pattern that you used earlier to match only words that contain secret but then continue with one or more word character (w+
). Only one of the companies in this fake dataset seems to operate secretly!
You can write any complex regex pattern and pass it to .str.contains()
to carve from your pandas column just the rows that you need for your analysis.
Conclusion
Like a persistent treasure hunter, you found each "secret"
, no matter how well it was hidden! In the process, you learned that the best way to check whether a string contains a substring in Python is to use the in
membership operator.
You also learned how to descriptively use two other string methods, which are often misused to check for substrings:
.count()
to count the occurrences of a substring in a string.index()
to get the index position of the beginning of the substring
After that, you explored how to find substrings according to more advanced conditions with regular expressions and a few functions in Python’s re
module.
Finally, you also learned how you can use the DataFrame method .str.contains()
to check which entries in a pandas DataFrame contain a substring .
You now know how to pick the most idiomatic approach when you’re working with substrings in Python. Keep using the most descriptive method for the job, and you’ll write code that’s delightful to read and quick for others to understand.
Watch Now This tutorial has a related video course created by the Real Python team. Watch it together with the written tutorial to deepen your understanding: Check if a Python String Contains a Substring
Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article
In this article, we are going to see how to search for a string in text files using Python
Example:
string = “GEEK FOR GEEKS”
Input: “FOR”
Output: Yes, FOR is present in the given string.
Text File for demonstration:
myfile.txt
Finding the index of the string in the text file using readline()
In this method, we are using the readline() function, and checking with the find() function, this method returns -1 if the value is not found and if found it returns 0.
Python3
with
open
(r
'myfile.txt'
,
'r'
) as fp:
lines
=
fp.readlines()
for
row
in
lines:
word
=
'Line 3'
if
row.find(word) !
=
-
1
:
print
(
'string exists in file'
)
print
(
'line Number:'
, lines.index(row))
Output:
string exists in file line Number: 2
Finding string in a text file using read()
we are going to search string line by line if the string is found then we will print that string and line number using the read() function.
Python3
with
open
(r
'myfile.txt'
,
'r'
) as
file
:
content
=
file
.read()
if
'Line 8'
in
content:
print
(
'string exist'
)
else
:
print
(
'string does not exist'
)
Output:
string does not exist
Search for a String in Text Files using enumerate()
We are just finding string is present in the file or not using the enumerate() in Python.
Python3
with
open
(r
"myfile.txt"
,
'r'
) as f:
for
index, line
in
enumerate
(f):
if
'Line 3y'
in
line:
print
(
'string found in a file'
)
break
print
(
'string does not exist in a file'
)
Output:
string does not exist in a file
Like Article
Save Article
In this Python tutorial, you’ll learn to search a string in a text file. Also, we’ll see how to search a string in a file and print its line and line number.
After reading this article, you’ll learn the following cases.
- If a file is small, read it into a string and use the
find()
method to check if a string or word is present in a file. (easier and faster than reading and checking line per line) - If a file is large, use the mmap to search a string in a file. We don’t need to read the whole file in memory, which will make our solution memory efficient.
- Search a string in multiple files
- Search file for a list of strings
We will see each solution one by one.
Table of contents
- How to Search for a String in Text File
- Example to search for a string in text file
- Search file for a string and Print its line and line number
- Efficient way to search string in a large text file
- mmap to search for a string in text file
- Search string in multiple files
- Search file for a list of strings
How to Search for a String in Text File
Use the file read()
method and string class find()
method to search for a string in a text file. Here are the steps.
- Open file in a read mode
Open a file by setting a file path and access mode to the
open()
function. The access mode specifies the operation you wanted to perform on the file, such as reading or writing. For example, r is for reading.fp= open(r'file_path', 'r')
- Read content from a file
Once opened, read all content of a file using the
read()
method. Theread()
method returns the entire file content in string format. - Search for a string in a file
Use the
find()
method of a str class to check the given string or word present in the result returned by theread()
method. Thefind()
method. The find() method will return -1 if the given text is not present in a file - Print line and line number
If you need line and line numbers, use the
readlines(
) method instead ofread()
method. Use the for loop andreadlines()
method to iterate each line from a file. Next, In each iteration of a loop, use the if condition to check if a string is present in a current line and print the current line and line number
Example to search for a string in text file
I have a ‘sales.txt’ file that contains monthly sales data of items. I want the sales data of a specific item. Let’s see how to search particular item data in a sales file.
def search_str(file_path, word):
with open(file_path, 'r') as file:
# read all content of a file
content = file.read()
# check if string present in a file
if word in content:
print('string exist in a file')
else:
print('string does not exist in a file')
search_str(r'E:demosfiles_demosaccountsales.txt', 'laptop')
Output:
string exists in a file
Search file for a string and Print its line and line number
Use the following steps if you are searching a particular text or a word in a file, and you want to print a line number and line in which it is present.
- Open a file in a read mode.
- Next, use the
readlines()
method to get all lines from a file in the form of a list object. - Next, use a loop to iterate each line from a file.
- Next, In each iteration of a loop, use the if condition to check if a string is present in a current line and print the current line and line number.
Example: In this example, we’ll search the string ‘laptop’ in a file, print its line along with the line number.
# string to search in file
word = 'laptop'
with open(r'E:demosfiles_demosaccountsales.txt', 'r') as fp:
# read all lines in a list
lines = fp.readlines()
for line in lines:
# check if string present on a current line
if line.find(word) != -1:
print(word, 'string exists in file')
print('Line Number:', lines.index(line))
print('Line:', line)
Output:
laptop string exists in a file line: laptop 10 15000 line number: 1
Note: You can also use the readline()
method instead of readlines()
to read a file line by line, stop when you’ve gotten to the lines you want. Using this technique, we don’t need to read the entire file.
Efficient way to search string in a large text file
All above way read the entire file in memory. If the file is large, reading the whole file in memory is not ideal.
In this section, we’ll see the fastest and most memory-efficient way to search a string in a large text file.
- Open a file in read mode
- Use for loop with
enumerate()
function to get a line and its number. Theenumerate()
function adds a counter to an iterable and returns it in enumerate object. Pass the file pointer returned by theopen()
function to theenumerate()
. - We can use this enumerate object with a for loop to access the each line and line number.
Note: The enumerate(file_pointer)
doesn’t load the entire file in memory, so this is an efficient solution.
Example:
with open(r"E:demosfiles_demosaccountsales.txt", 'r') as fp:
for l_no, line in enumerate(fp):
# search string
if 'laptop' in line:
print('string found in a file')
print('Line Number:', l_no)
print('Line:', line)
# don't look for next lines
break
Example:
string found in a file Line Number: 1 Line: laptop 10 15000
mmap to search for a string in text file
In this section, we’ll see the fastest and most memory-efficient way to search a string in a large text file.
Also, you can use the mmap module to find a string in a huge file. The mmap.mmap()
method creates a bytearray
object that checks the underlying file instead of reading the whole file in memory.
Example:
import mmap
with open(r'E:demosfiles_demosaccountsales.txt', 'rb', 0) as file:
s = mmap.mmap(file.fileno(), 0, access=mmap.ACCESS_READ)
if s.find(b'laptop') != -1:
print('string exist in a file')
Output:
string exist in a file
Search string in multiple files
Sometimes you want to search a string in multiple files present in a directory. Use the below steps to search a text in all files of a directory.
- List all files of a directory
- Read each file one by one
- Next, search for a word in the given file. If found, stop reading the files.
Example:
import os
dir_path = r'E:demosfiles_demosaccount'
# iterate each file in a directory
for file in os.listdir(dir_path):
cur_path = os.path.join(dir_path, file)
# check if it is a file
if os.path.isfile(cur_path):
with open(cur_path, 'r') as file:
# read all content of a file and search string
if 'laptop' in file.read():
print('string found')
break
Output:
string found
Search file for a list of strings
Sometimes you want to search a file for multiple strings. The below example shows how to search a text file for any words in a list.
Example:
words = ['laptop', 'phone']
with open(r'E:demosfiles_demosaccountsales.txt', 'r') as f:
content = f.read()
# Iterate list to find each word
for word in words:
if word in content:
print('string exist in a file')
Output:
string exist in a file
Python Exercises and Quizzes
Free coding exercises and quizzes cover Python basics, data structure, data analytics, and more.
- 15+ Topic-specific Exercises and Quizzes
- Each Exercise contains 10 questions
- Each Quiz contains 12-15 MCQ
When you’re working with a Python program, you might need to search for and locate a specific string inside another string.
This is where Python’s built-in string methods come in handy.
In this article, you will learn how to use Python’s built-in find()
string method to help you search for a substring inside a string.
Here is what we will cover:
- Syntax of the
find()
method- How to use
find()
with no start and end parameters example - How to use
find()
with start and end parameters example - Substring not found example
- Is the
find()
method case-sensitive?
- How to use
find()
vsin
keywordfind()
vsindex()
The find()
Method — A Syntax Overview
The find()
string method is built into Python’s standard library.
It takes a substring as input and finds its index — that is, the position of the substring inside the string you call the method on.
The general syntax for the find()
method looks something like this:
string_object.find("substring", start_index_number, end_index_number)
Let’s break it down:
string_object
is the original string you are working with and the string you will call thefind()
method on. This could be any word you want to search through.- The
find()
method takes three parameters – one required and two optional. "substring"
is the first required parameter. This is the substring you are trying to find insidestring_object
. Make sure to include quotation marks.start_index_number
is the second parameter and it’s optional. It specifies the starting index and the position from which the search will start. The default value is0
.end_index_number
is the third parameter and it’s also optional. It specifies the end index and where the search will stop. The default is the length of the string.- Both the
start_index_number
and theend_index_number
specify the range over which the search will take place and they narrow the search down to a particular section.
The return value of the find()
method is an integer value.
If the substring is present in the string, find()
returns the index, or the character position, of the first occurrence of the specified substring from that given string.
If the substring you are searching for is not present in the string, then find()
will return -1
. It will not throw an exception.
How to Use find()
with No Start and End Parameters Example
The following examples illustrate how to use the find()
method using the only required parameter – the substring you want to search.
You can take a single word and search to find the index number of a specific letter:
fave_phrase = "Hello world!"
# find the index of the letter 'w'
search_fave_phrase = fave_phrase.find("w")
print(search_fave_phrase)
#output
# 6
I created a variable named fave_phrase
and stored the string Hello world!
.
I called the find()
method on the variable containing the string and searched for the letter ‘w’ inside Hello world!
.
I stored the result of the operation in a variable named search_fave_phrase
and then printed its contents to the console.
The return value was the index of w
which in this case was the integer 6
.
Keep in mind that indexing in programming and Computer Science in general always starts at 0
and not 1
.
How to Use find()
with Start and End Parameters Example
Using the start and end parameters with the find()
method lets you limit your search.
For example, if you wanted to find the index of the letter ‘w’ and start the search from position 3
and not earlier, you would do the following:
fave_phrase = "Hello world!"
# find the index of the letter 'w' starting from position 3
search_fave_phrase = fave_phrase.find("w",3)
print(search_fave_phrase)
#output
# 6
Since the search starts at position 3, the return value will be the first instance of the string containing ‘w’ from that position and onwards.
You can also narrow down the search even more and be more specific with your search with the end parameter:
fave_phrase = "Hello world!"
# find the index of the letter 'w' between the positions 3 and 8
search_fave_phrase = fave_phrase.find("w",3,8)
print(search_fave_phrase)
#output
# 6
Substring Not Found Example
As mentioned earlier, if the substring you specify with find()
is not present in the string, then the output will be -1
and not an exception.
fave_phrase = "Hello world!"
# search for the index of the letter 'a' in "Hello world"
search_fave_phrase = fave_phrase.find("a")
print(search_fave_phrase)
# -1
Is the find()
Method Case-Sensitive?
What happens if you search for a letter in a different case?
fave_phrase = "Hello world!"
#search for the index of the letter 'W' capitalized
search_fave_phrase = fave_phrase.find("W")
print(search_fave_phrase)
#output
# -1
In an earlier example, I searched for the index of the letter w
in the phrase «Hello world!» and the find()
method returned its position.
In this case, searching for the letter W
capitalized returns -1
– meaning the letter is not present in the string.
So, when searching for a substring with the find()
method, remember that the search will be case-sensitive.
The find()
Method vs the in
Keyword – What’s the Difference?
Use the in
keyword to check if the substring is present in the string in the first place.
The general syntax for the in
keyword is the following:
substring in string
The in
keyword returns a Boolean value – a value that is either True
or False
.
>>> "w" in "Hello world!"
True
The in
operator returns True
when the substring is present in the string.
And if the substring is not present, it returns False
:
>>> "a" in "Hello world!"
False
Using the in
keyword is a helpful first step before using the find()
method.
You first check to see if a string contains a substring, and then you can use find()
to find the position of the substring. That way, you know for sure that the substring is present.
So, use find()
to find the index position of a substring inside a string and not to look if the substring is present in the string.
The find()
Method vs the index()
Method – What’s the Difference?
Similar to the find()
method, the index()
method is a string method used for finding the index of a substring inside a string.
So, both methods work in the same way.
The difference between the two methods is that the index()
method raises an exception when the substring is not present in the string, in contrast to the find()
method that returns the -1
value.
fave_phrase = "Hello world!"
# search for the index of the letter 'a' in 'Hello world!'
search_fave_phrase = fave_phrase.index("a")
print(search_fave_phrase)
#output
# Traceback (most recent call last):
# File "/Users/dionysialemonaki/python_article/demopython.py", line 4, in <module>
# search_fave_phrase = fave_phrase.index("a")
# ValueError: substring not found
The example above shows that index()
throws a ValueError
when the substring is not present.
You may want to use find()
over index()
when you don’t want to deal with catching and handling any exceptions in your programs.
Conclusion
And there you have it! You now know how to search for a substring in a string using the find()
method.
I hope you found this tutorial helpful.
To learn more about the Python programming language, check out freeCodeCamp’s Python certification.
You’ll start from the basics and learn in an interactive and beginner-friendly way. You’ll also build five projects at the end to put into practice and help reinforce your understanding of the concepts you learned.
Thank you for reading, and happy coding!
Happy coding!
Learn to code for free. freeCodeCamp’s open source curriculum has helped more than 40,000 people get jobs as developers. Get started
In this Python tutorial, we will discuss everything on Python find substring in string with a few more examples.
Python provides several methods to find substrings in a string. Here we will discuss 12 different methods to check if Python String contains a substring.
- Using the in operator
- Using The find() method
- Using the index() method
- Using the re module
- Using the startswith() method
- Using the endswith() method
- Using the split() method
- Using the partition() method
- Using the count() method
- Using the rfind() method
- Using the list comprehension
- Using the re.findall()
Method-1: Using the in operator
The in operator is one of the simplest and quickest ways to check if a substring is present in a string. It returns True if the substring is found and False otherwise.
# Define the main string
string = "I live in USA"
# Define the substring to be searched
substring = "USA"
# Use 'in' operator to check if substring is present in string
if substring in string:
print("Substring found")
else:
print("Substring not found")
The above code checks if a given substring is present in a given string.
- The main string is stored in the variable string and the substring to be searched is stored in the variable substring.
- The code uses the in operator to check if the substring is present in the string. If it is, the code outputs “Substring found” to the console. If not, the code outputs “Substring not found”.
Read: Slicing string in Python + Examples
Method-2: Using The find() method
The find()
method is another simple way to find substrings in a string. It returns the index of the first occurrence of the substring in the string. If the substring is not found, it returns -1.
# Define the main string
string = "I live in USA"
# Define the substring to be searched
substring = "USA"
# Use the find() method to get the index of the substring
index = string.find(substring)
# Check if the substring is found
if index != -1:
print("Substring found at index", index)
else:
print("Substring not found")
The above code uses the find() method to search for the index of a given substring in a given string.
- The find() method is used to search for the index of the substring in the string, and the result is stored in the variable index.
- If the substring is found, the index will be set to the index of the first character of the substring in the string. If the substring is not found, index will be set to -1.
- The code then checks if index is not equal to -1. If it is not, the substring was found and the code outputs “Substring found at index” followed by the value of the index.
- If the index is equal to -1, the substring was not found and the code outputs “Substring not found”.
Read: Convert string to float in Python
Method-3: Using the index() method
The index() method is similar to the find() method, but it raises a ValueError exception if the substring is not found in the string.
# Search for substring in a given string
string = "I live in USA"
# The substring we want to search for
substring = "live"
# Use try-except block to handle potential ValueError if substring is not found
try:
# Find the index of the substring in the string using the index() method
index = string.index(substring)
# Print a success message with the index of the substring
print("Substring found at index", index)
except ValueError:
# If the substring is not found, print a message indicating that it was not found
print("Substring not found")
The code above is checking if a given substring is present in a string.
- The input string is “I live in USA” and the substring we want to search for is “live”.
- The code uses a try-except block to handle the potential error of not finding the substring in the string.
- The index() method is used to find the index of the substring in the string. If the substring is found, the code prints a message indicating the index of the substring in the string.
- If the substring is not found, a ValueError is raised, which is caught by the except block, and a message indicating that the substring was not found is printed.
Read: Append to a string Python + Examples
Method-4: Using the re module
The re (regular expression) module provides powerful methods for matching and searching for substrings in a string.
# Use the re module for pattern matching
import re
# The string we want to search in
string = "I live in USA"
# The substring we want to search for
substring = "in"
# Use the search() method from the re module to find a match
match = re.search(substring, string)
# Check if a match was found
if match:
# If a match was found, print a success message
print("Substring found")
else:
# If no match was found, print a failure message
print("Substring not found")
The code above is checking if a given substring is present in a string using regular expressions (regex).
- The first line import re imports the re module which provides functions for pattern matching in strings.
- The input string is “I live in USA” and the substring we want to search for is “in”. The code then uses the re.search() method to find a match between the substring and the input string.
- The re.search() method returns a match object if there is a match between the substring and the input string, otherwise it returns None.
- The code then uses an if statement to check if a match was found. If a match was found, the code prints a message indicating that the substring was found.
- If no match was found, the code prints a message indicating that the substring was not found.
Read: Python compare strings
Method-5: Using the startswith() method
The startswith() method returns True if the string starts with the specified substring and False otherwise.
# The string we want to search in
string = "I live in USA"
# The substring we want to search for
substring = "I"
# Use the startswith() method to check if the string starts with the substring
if string.startswith(substring):
# If the string starts with the substring, print a success message
print("Substring found")
else:
# If the string does not start with the substring, print a failure message
print("Substring not found")
The code above checks if a given substring is at the beginning of a string.
- The input string is “I live in USA” and the substring we want to search for is “I”. The code uses the startswith() method to check if the input string starts with the substring.
- The startswith() method returns True if the input string starts with the substring and False otherwise. The code then uses an if statement to check the result of the startswith() method.
- If the input string starts with the substring, the code prints a message indicating that the substring was found. If the input string does not start with the substring, the code prints a message indicating that the substring was not found.
Read: Python program to reverse a string with examples
Method-6: Using the endswith() method
The endswith() method is similar to the startswith() method, but it returns True if the string ends with the specified substring and False otherwise.
# The string we want to search in
string = "I live in USA"
# The substring we want to search for
substring = "USA"
# Use the endswith() method to check if the string ends with the substring
if string.endswith(substring):
# If the string ends with the substring, print a success message
print("Substring found")
else:
# If the string does not end with the substring, print a failure message
print("Substring not found")
The code above checks if a given substring is at the end of a string.
- The input string is “I live in USA” and the substring we want to search for is “USA”. The code uses the endswith() method to check if the input string ends with the substring.
- The endswith() method returns True if the input string ends with the substring and False otherwise. The code then uses an if statement to check the result of the endswith() method.
- If the input string ends with the substring, the code prints a message indicating that the substring was found. If the input string does not end with the substring, the code prints a message indicating that the substring was not found.
Read: Python string formatting with examples.
Method-7: Using the split() method
The split() method splits a string into a list of substrings based on a specified delimiter. The resulting substrings can then be searched for the desired substring.
# The string we want to search in
string = "I live in USA"
# The substring we want to search for
substring = "USA"
# Split the string into substrings using the split() method and store the result in a list
substrings = string.split(" ")
# Check if the substring is in the list of substrings
if substring in substrings:
# If the substring is in the list, print a success message
print("Substring found")
else:
# If the substring is not in the list, print a failure message
print("Substring not found")
The code above checks if a given substring is contained within a string.
- The input string is “I live in USA” and the substring we want to search for is “USA”. The code splits the input string into substrings using the split() method and stores the result in a list substrings.
- The split() method splits a string into substrings using a specified delimiter (in this case, a space character).
- Next, the code uses the
in
operator to check if the substring is in the list of substrings. If the substring is in the list, the code prints a message indicating that the substring was found. - If the substring is not in the list, the code prints a message indicating that the substring was not found.
Method-8: Using the partition() method
The partition() method splits a string into a tuple of three substrings: the substring before the specified delimiter, the specified delimiter, and the substring after the specified delimiter.
# The string we want to search in
string = "I live in USA"
# The substring we want to search for
substring = "I"
# Use the partition() method to split the string into three parts
before, delimiter, after = string.partition(" ")
# Check if the first part of the split string is equal to the substring
if before == substring:
# If the first part is equal to the substring, print a success message
print("Substring found")
else:
# If the first part is not equal to the substring, print a failure message
print("Substring not found")
The code above checks if a given substring is at the beginning of a string.
The input string is “I live in USA” and the substring we want to search for is “I”. The code uses the partition() method to split the input string into three parts:
- The part before the specified delimiter, the delimiter itself, and the part after the delimiter. In this case, the delimiter is a space character.
- The partition() method returns a tuple with three elements: the part before the delimiter, the delimiter itself, and the part after the delimiter.
- The code uses tuple unpacking to assign the three parts to the variables before, delimiter, and after.
- Next, the code uses an if statement to check if the first part of the split string (i.e., before) is equal to the substring.
- If the first part is equal to the substring, the code prints a message indicating that the substring was found. If the first part is not equal to the substring, the code prints a message indicating that the substring was not found.
Method-9: Using the count() method
The count() method returns the number of times a substring appears in a string.
# The string we want to search in
string = "I live in USA"
# The substring we want to search for
substring = "live"
# Use the count() method to count the number of times the substring appears in the string
count = string.count(substring)
# Print the result
print("Substring found", count, "times")
The code above counts the number of times a given substring appears in a string.
- The input string is “I live in USA” and the substring we want to search for is “live”. The code uses the count() method to count the number of times the substring appears in the string.
- Finally, the code uses the print() function to print the result, indicating how many times the substring was found in the string.
Method-10: Using the rfind() method
The rfind() method is similar to the find() method, but it returns the index of the last occurrence of the substring in the string. If the substring is not found, it returns -1.
# The string we want to search in
string = "I live in USA"
# The substring we want to search for
substring = "USA"
# Use the rfind() method to find the last index of the substring in the string
index = string.rfind(substring)
# Check if the substring was found
if index != -1:
# If the substring was found, print the index
print("Substring found at index", index)
else:
# If the substring was not found, print a message
print("Substring not found")
The code above searches for the last occurrence of a given substring in a string.
- The input string is “I live in USA” and the substring we want to search for is “USA”. The code uses the rfind() method to find the last index of the substring in the string.
- The rfind() method returns the index of the last occurrence of the substring in the string, or -1 if the substring is not found. So the code checks if the returned value is not equal to -1, indicating that the substring was found in the string.
- Finally, the code uses the print() function to print the result, indicating the index of the last occurrence of the substring in the string.
Method-11: Using the list comprehension
# The string we want to search in
string = "I live in USA"
# The substring we want to search for
substring = "USA"
# Use a list comprehension to find if the substring exists in the string split into words
result = [word for word in string.split() if word == substring]
# Check if the result list is not empty
if result:
# If the result list is not empty, the substring was found
print("Substring found")
else:
# If the result list is empty, the substring was not found
print("Substring not found")
The code above checks if a given substring exists in a string.
- The input string is “I live in USA” and the substring we want to search for is “USA”. The code uses a list comprehension to create a list of words from the input string, where each word is checked if it is equal to the given substring.
- The list comprehension iterates over the words in the input string, which is split using the split() method, and adds the word to the result list if it is equal to the given substring.
- Finally, the code uses an if statement to check if the result list is not empty. If the result list is not empty, it means that the substring was found in the input string, so the code prints “Substring found”.
- If the result list is empty, the substring was not found in the input string, so the code prints “Substring not found”.
Method-12: Using the re.findall()
The re.findall() function returns a list of all non-overlapping matches of the specified pattern within the string. We can use this function to find all occurrences of a substring within a string by specifying the substring as the pattern.
# Import the regular expression library 're'
import re
# The input text to search for the substring
text = "I live in USA"
# The substring to search for in the text
substring = "USA"
# Find all occurrences of the substring in the text using the 'findall' method from the 're' library
result = re.findall(substring, text)
# Print the result
print(result)
In the above code, the regular expression library re is imported and used to find all occurrences of the given substring “USA” in the input text “I live in USA”.
- The re.findall method is used to search for all occurrences of the substring in the text and return them as a list.
- Finally, the result is printed on the console.
You may like the following Python examples:
- How to concatenate strings in python
- Find Last Number in String in Python
- Find first number in string in Python
In this Python tutorial, we learned, Python find substring in string using the below methods:
- Python find substring in string using the in operator
- Python find substring in string using The find() method
- Python find substring in string using the index() method
- Python find substring in string using the re module
- Python find substring in string using the startswith() method
- Python find substring in string using the endswith() method
- Python find substring in string using the split() method
- Python find substring in string using the partition() method
- Python find substring in string using the count() method
- Python find substring in string using the rfind() method
- Python find substring in string using the list comprehension
- Python find substring in string using the re.findall()
Python is one of the most popular languages in the United States of America. I have been working with Python for a long time and I have expertise in working with various libraries on Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… I have experience in working with various clients in countries like United States, Canada, United Kingdom, Australia, New Zealand, etc. Check out my profile.
В этой статье поговорим про строки в Python, особенности поиска, а также о том, как искать подстроку или символ в строке. Но сначала давайте вспомним основные методы для обработки строк в Python:
• isalpha(str): если строка в Python включает в себя лишь алфавитные символы, возвращается True;
• islower(str): True возвращается, если строка включает лишь символы в нижнем регистре;
• isupper(str): True, если символы строки в Python находятся в верхнем регистре;
• startswith(str): True, когда строка начинается с подстроки str;
• isdigit(str): True, когда каждый символ строки — цифра;
• endswith(str): True, когда строка в Python заканчивается на подстроку str;
• upper(): строка переводится в верхний регистр;
• lower(): строка переводится в нижний регистр;
• title(): для перевода начальных символов всех слов в строке в верхний регистр;
• capitalize(): для перевода первой буквы самого первого слова строки в верхний регистр;
• lstrip(): из строки в Python удаляются начальные пробелы;
• rstrip(): из строки в Python удаляются конечные пробелы;
• strip(): из строки в Python удаляются и начальные, и конечные пробелы;
• rjust(width): когда длина строки меньше, чем параметр width, слева добавляются пробелы, строка выравнивается по правому краю;
• ljust(width): когда длина строки в Python меньше, чем параметр width, справа от неё добавляются пробелы для дополнения значения width, при этом происходит выравнивание строки по левому краю;
• find(str[, start [, end]): происходит возвращение индекса подстроки в строку в Python. В том случае, если подстрока не найдена, выполняется возвращение числа -1;
• center(width): когда длина строки в Python меньше, чем параметр width, слева и справа добавляются пробелы (равномерно) для дополнения значения width, причём происходит выравнивание строки по центру;
• split([delimeter[, num]]): строку в Python разбиваем на подстроки в зависимости от разделителя;
• replace(old, new[, num]): в строке одна подстрока меняется на другую;
• join(strs): строки объединяются в одну строку, между ними вставляется определённый разделитель.
Обрабатываем строку в Python
Представим, что ожидается ввод числа с клавиатуры. Перед преобразованием введенной нами строки в число можно легко проверить, введено ли действительно число. Если это так, выполнится операция преобразования. Для обработки строки используем такой метод в Python, как isnumeric():
string = input("Введите какое-нибудь число: ") if string.isnumeric(): number = int(string) print(number)Следующий пример позволяет удалять пробелы в конце и начале строки:
string = " привет мир! " string = string.strip() print(string) # привет мир!Так можно дополнить строку пробелами и выполнить выравнивание:
print("iPhone 7:", "52000".rjust(10)) print("Huawei P10:", "36000".rjust(10))В консоли Python будет выведено следующее:
iPhone 7: 52000 Huawei P10: 36000Поиск подстроки в строке
Чтобы в Python выполнить поиск в строке, используют метод find(). Он имеет три формы и возвращает индекс 1-го вхождения подстроки в строку:
• find(str): поиск подстроки str производится с начала строки и до её конца;
• find(str, start): с помощью параметра start задаётся начальный индекс, и именно с него и выполняется поиск;
• find(str, start, end): посредством параметра end задаётся конечный индекс, поиск выполняется до него.Когда подстрока не найдена, метод возвращает -1:
welcome = "Hello world! Goodbye world!" index = welcome.find("wor") print(index) # 6 # ищем с десятого индекса index = welcome.find("wor",10) print(index) # 21 # ищем с 10-го по 15-й индекс index = welcome.find("wor",10,15) print(index) # -1Замена в строке
Чтобы в Python заменить в строке одну подстроку на другую, применяют метод replace():
• replace(old, new): подстрока old заменяется на new;
• replace(old, new, num): параметр num показывает, сколько вхождений подстроки old требуется заменить на new.Пример замены в строке в Python:
phone = "+1-234-567-89-10" # дефисы меняются на пробелы edited_phone = phone.replace("-", " ") print(edited_phone) # +1 234 567 89 10 # дефисы удаляются edited_phone = phone.replace("-", "") print(edited_phone) # +12345678910 # меняется только первый дефис edited_phone = phone.replace("-", "", 1) print(edited_phone) # +1234-567-89-10Разделение на подстроки в Python
Для разделения в Python используется метод split(). В зависимости от разделителя он разбивает строку на перечень подстрок. В роли разделителя в данном случае может быть любой символ либо последовательность символов. Этот метод имеет следующие формы:
• split(): в роли разделителя применяется такой символ, как пробел;
• split(delimeter): в роли разделителя применяется delimeter;
• split(delimeter, num): параметром num указывается, какое количество вхождений delimeter применяется для разделения. При этом оставшаяся часть строки добавляется в перечень без разделения на подстроки.Соединение строк в Python
Рассматривая простейшие операции со строками, мы увидели, как объединяются строки через операцию сложения. Однако есть и другая возможность для соединения строк — метод join():, объединяющий списки строк. В качестве разделителя используется текущая строка, у которой вызывается этот метод:
words = ["Let", "me", "speak", "from", "my", "heart", "in", "English"] # символ разделителя - пробел sentence = " ".join(words) print(sentence) # Let me speak from my heart in English # символ разделителя - вертикальная черта sentence = " | ".join(words) print(sentence) # Let | me | speak | from | my | heart | in | EnglishА если вместо списка в метод join передать простую строку, разделитель будет вставляться уже между символами:
word = "hello" joined_word = "|".join(word) print(joined_word) # h|e|l|l|o