Check if a Substring is Present in a Given String in Python

By Lenin Mishra

If you prefer to watch Youtube videos over reading blogs, check out our video on Python strings here.

Given two strings, check if s1 is present in s2.

Method 1 - Using in and not in keywords

Code

s2 = "I like Pylenin"
s1 = 'like'

print(s1 in s2)
print(s1 not in s2)

Output

True
False

Method 2 - Python String find() Method

The find() method in Python, finds the first occurrence of a specified value in a given string. It returns the index where the substring s1 occurs. It returns -1 if the value is not found.

The find() method is similar to the index() method. Only difference - index() method raises an exception if the value is not found.

Code

s2 = "I like Pylenin"
s1 = 'like'

print(s2.find(s1))

Output

2

The above result tells us that the like substring first occurs at 2nd index.

Let’s check for another substring.

Code

s2 = "I like Pylenin"
s1 = 'Python'

print(s2.find(s1))

Output

-1

The above result tells us that the substring Python doesn’t exist in s1.

Method 3 - Python String count() Method

The count() method returns the number of times a substring s1 appears in the string s2. It is not case sensitive.

Syntax

string.count(value, start, end)

value:	The substring to search for(required)
start:	The integer position to start the search. Default is 0.(Optional)
end:	The integer position to end the search. Default is the end of the string.(Optional)

Code

s2 = "I like Pylenin"
s1 = 'like'

if s2.count(s1) > 0:
    print(f"'{s1}' exists in '{s2}'")

Output

'like' exists in 'I like Pylenin'

Let’s search for a non-existing string.

Code

s2 = "I like Pylenin"
s1 = 'Python'

if s2.count(s1) > 0:
    print(f"'{s1}' exists in '{s2}'")
else:
    print(f"'{s1}' doesn't exist in '{s2}'")

Output

'Python' doesn't exist in 'I like Pylenin'

Let’s now search a substring s1 in a specific portion of string s2.

Code

s2 = "I like reading Pylenin blogs on Python"
s1 = 'Python'

# Search between position 10 and 20
if s2.count(s1, 10, 20) > 0:
    print(f"'{s1}' exists in '{s2}'")
else:
    print(f"'{s1}' doesn't exist in '{s2}'")

print(s2.find(s1))

Output

'Python' doesn't exist in 'I like reading Pylenin blogs on Python'
32

As you can see, the substring Python doesn’t occur between the positions 10 and 20. The find() method shows that it occurs at position 32.

  1. How to create a string in Python?
  2. How to access characters in a Python string?
  3. How to replace characters in a string in Python?
  4. How to concatenate strings in Python?
  5. How to iterate through a string in Python?
  6. Escape sequences in Python String
  7. Python String Formatting - The Definitive Guide
  8. Commonly used Python string methods