Substring search in Python

+4 votes
Is there a function in Python to search for a string within a string in Python?
Apr 13, 2018 in Python by kaalabilli
• 1,090 points

6 answers to this question.

+2 votes

Use the "in" operator in python"

if "substring" not in string1:
    #continue code
answered Apr 13, 2018 by Nietzsche's daemon
• 4,260 points
+1 vote
string = "Hello Agnosticdev, I love Tutorials"
substring = "Agnosticdev"
# Straight forward approach for Python 2.7 and Python 3.6
# Executes the conditional statement when the substring is found
if substring in string:
	print ("Your substring was found!")
answered Oct 12, 2018 by findingbugs
• 4,790 points
+1 vote

You can use the in operator:

if "blah" not in somestring: 
answered Oct 12, 2018 by abc
+1 vote

If it's just a substring search you can use string.find("substring").

You do have to be a little careful with find, index, and in though, as they are substring searches. In other words, this:

s = "This be a string"
if s.find("is") == -1:
    print "No 'is' here!"
    print "Found 'is' in the string."

It would print Found 'is' in the string. Similarly, if "is" in s: would evaluate to True. This may or may not be what you want.

answered Oct 12, 2018 by rani
+1 vote

str.find() method

The second method is to use the str.find() method. Here, we call the .find() method on the string in which substring is to found. We pass the substring to the find() method and check its return value. If its value is other than -1, the substring was found in the string, otherwise not. The value returned is the index where substring was found.

>>> some_string = "valar morghulis"

>>> some_string.find("morghulis")

>>> some_string.find("dohaeris")
answered Oct 12, 2018 by kalpesh
0 votes
You can simply use a if statement like you do for checking a condition.
Here also you can check if a substring is present in a string and if it is true continue with the remaining logic
answered Feb 14, 2019 by Shashank
• 1,370 points

Related Questions In Python

0 votes
1 answer

how to use Binary tree search in Python

The key thing to notice is that ...READ MORE

answered Sep 27, 2018 in Python by Priyaj
• 57,700 points
0 votes
1 answer

In python how to test a string for a substring

if "ABCD" in "xxxxABCDyyyy": This can be used ...READ MORE

answered Oct 24, 2018 in Python by Priyaj
• 57,700 points
+1 vote
2 answers

how can i do a binary search in python?

def Binary_search (array , element):       ...READ MORE

answered Jul 10 in Python by anonymous
• 240 points
0 votes
1 answer

How to search for a word in a string in Python?

You can do it like this: txt = ...READ MORE

answered Apr 4, 2019 in Python by Kumar
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 1,474 views
0 votes
0 answers
+4 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,800 points
+4 votes
7 answers

Splitting a List into chunks in Python

Here's a generator that yields the chunks ...READ MORE

answered Apr 13, 2018 in Python by Nietzsche's daemon
• 4,260 points
+2 votes
6 answers

Slicing in Python

Index: ------------> ...READ MORE

answered Oct 18, 2018 in Python by abc