Change language

Python | Check if substring is present in string

|

Method 1: Using the in operator
in operators is the most versatile, quickest way to check substrings, cardinality in operators in python are very well known and are used in many operations throughout the language.

# Python 3 demo code
# check a substring in a string
# use in a statement

  
# initialization string

test_str = "GeeksforGeeks"

 
# use for checking
# for substring

print ( "Does for exists in GeeksforGeeks? : " )

if " for " in test_str:

  print ( "Yes, String found" )

else

print ( "No, String not found " )

Output:

 Does for exists in GeeksforGeeks?: Yes, String found 

Method 2: Using str.find ()
The str.find () method is usually used to get the smallest index at which a string occurs, but also returns -1 if the row is missing, therefore, if any value returns" = 0, line present, otherwise missing.

# Python 3 demo code
# checking a substring in a string
# using str.find ()

 
# initialization string

test_str = "GeeksforGeeks"

 
# using str.find () to check
# for substring

res = test_str.find ( "for" )

if res" = 0 :

  print ( " for is present in Python.Engineering " )

else :

print ( " for is not present in Python.Engineering " )

Output:

 for is present in Python.Engineering 

Method 3: use str.index()
This method can be used to perform a similar task, but like str.find (), it does not return a value, but a ValueError if the string is missing , so catching an exception is the way to test a string in a substring.

 

# Python 3 demo code
# checking a substring in a string
# using str.index ()

 
# initialization string

test_str = "GeeksforGeeks"

 
# using str.index () to check
# for substring

try

res = test_str .index ( "forg" )

  print ( " forg exists in Python.Engineering " )

except :

print ( "forg does not exists in GeeksforGeeks" )

Output:

 forg does not exists in Python.Engineering 

Method 4: Use operator.contains ()
This is a lesser known method for checking a substring in a string, this method is also effective for this task of checking a string in a string.

# Python 3 demo code
# check and substrings in the string
# using operator.contains ()

import operator

 
# initializing string

test_str = "GeeksforGeeks"

 
# using operator.contains () to check
# for substring

if operator.contains (test_str, "for" ):

print ( "for is present in GeeksforGeeks" )

else :

print ( "for is not present in GeeksforGeeks" )

Output:

 for is present in Python.Engineering 

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically