How to Check if String Contains Substring in Python

Itsmycode·2021년 9월 5일
1
post-thumbnail
post-custom-banner

A substring is a sequence of characters in a given string. Python has several built-in methods that can help find and replace a substring. Following are the options available in Python to check if string contains substring.

  1. using find() method
  2. using in operator
  3. using count() method
  4. using str.index() method
  5. using operator.contains() method

Check if String Contains Substring in Python

Let’s look at all the different ways in Python to check if a string contains a substring.

using find() method

find() method is to verify if a string contains a substring or not. If the string contains a substring, it returns the starting index of the substring; else returns -1 if it cannot find a substring.

Syntax: string.find(substring, start, end)

Parameters:

  • substring: substring that needs to be searched in a given string
  • start (Optional): Starting position where the substring needs to be searched within the string.
  • end(Optional): Ending position where suffix needs to be searched within the string.

Note: If start and end indexes are not provided, then by default, it takes 0 as start index and length-1 as end index.

word = 'Hello its a beautiful day'

# returns first occurrence of Substring
result = word.find('beautiful ')
print ("Substring 'beautiful ' found at index:", result )  

# Substring is searched with start index 2
print(word.find('its', 2)) 

# Substring is searched with start index 10
print(word.find('its', 10)) 

# Substring is searched between start index 4 and end index 10
print(word.find('its', 4, 10)) 

# Substring is searched start index 10 and end index 20
print(word.find('its ', 10, 20))

# How to use find()
if (word.find('sunny') != -1):
    print ("Contains given substring ")
else:
    print ("Doesn't contains given substring")

Output

Substring 'beautiful ' found at index: 12
6
-1
6
-1
Doesn't contains given substring

Using in operator

The “ in ” operator checks if a substring is present within a string, returns TRUE if found else returns FALSE.

word = "Hello its a beautiful day"
sub1="beautiful"
sub2="sunny"

print(sub1 in word) 
print(sub2 in word)

#Output
True
False

using count() method

The count() method checks for the occurrence of a substring in a string; if not found, return 0.

word = "Hello its a beautiful day"
sub1="beautiful"
sub2="Hello"
sub3="Sunny"

print(word.count(sub1)) 
print(word.count(sub2)) 
print(word.count(sub3)) 

# Output
1
1
0

using str.index() method

The method checks that the given substring is present in a string. If the substring is not present in the string, it doesn’t return any value rather generates a ValueError.

Syntax: string.index(substring)

word = "Hello its a beautiful day"
try :  
    result = word.index("beautiful") 
    print ("beautiful is present in the string.") 
except : 
    print ("beautiful is not present in the string.") 

# Output
beautiful is present in the string.

using operator.contains() method

Using the operator module, we can search if the substring is present in a string.

Syntax: operator.contains(string,substring)

import operator 

word = "Hello its a beautiful day"
if operator.contains(word, "beautiful"): 
    print ("beautiful is present in the string.") 
else : 
    print ("beautiful is not present in the string.")

# Output
beautiful is present in the string.

The post How to Check if String Contains Substring in Python appeared first on ItsMyCode.

profile
Tech Blogger
post-custom-banner

0개의 댓글