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
124 views

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,750 points
+1 vote

You can use the in operator:

if "blah" not in somestring: 
    continue
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!"
else:
    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")
6

>>> some_string.find("dohaeris")
-1
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 by Shashank
• 1,350 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
• 56,920 points
83 views
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
• 56,920 points
95 views
0 votes
0 answers

how can i do a binary search in python?

can you give an example? READ MORE

Apr 2 in Python by Waseem
• 4,470 points
26 views
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 in Python by Kumar
40 views
+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 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 346 views
+4 votes
6 answers
+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
7,538 views
+2 votes
6 answers

Slicing in Python

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

answered Oct 18, 2018 in Python by abc
127 views