Python String Contains an Example — pythonpip.com

in this post, I am going to help you check if the string contains substring with example. The string is the main package of python. We will check a word or character is found or not in a python string.

This is the most common operations that developers use on strings is to check whether a string contains some other string.

Python has an inbuilt method like find, index, etc. These methods help to find the substring and character in the source string.

The following are the methods in Python to check if a string contains another substring:

  • Find Substring Using in operator
  • By using find() method
  • Check Substring Using not in method
  • Find Substring Using str.index() method
  • Check Substring Using operator.contains() method

Python String Contains Example

Let’s create a sample example to check a string contains a substring.

in Operator

The python has in operator to check character or substring into the string. The in operator returns the True value when the substring exists in the string. Otherwise, it returns false. You can use the in operator.

It also works with lists for checking if an item exists in a list. it’s not null-safe.

Syntax: substring in source_string

Example Using python in Operator

s = "This is a boy" "is" in s True "Is" in s False

It would print True if 'is' found in the source string otherwise False.

Output:

True

How To use python find() method

The find() function in Python can be used to check for substrings in a string. The find() function in Python is used to locate the index of a substring within a string.

Syntax: string.find(substring)

Example Using python find()

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

Output:

Found 'is' in the string.

Python not in operator

Let’s see a not in operator in Python with an example.

Python contains String using not in Operator

s = "I love python programming." if "love" not in s: print('is not exist in the String.') else: print('It exists.')

Output:

It exists.

Python index() method

Python String index() is a function that returns the index of a substring. The index() method finds the first occurrence of the substring into the source string. The index() method raises the exception if the value is not found.

An index() method is almost the same as the find() method, the only difference is that find() method returns -1 if that value is not found.

Syntax:

string.index(substring)

s = "I love python programming." isexist = s.index('love') print(isexist)

Output:

2

The python index returns the lowest index of the found substring. If substring doesn’t exist inside the string, it raises a ValueError exception. This method is useful when you want to know the index position of the substring.

operator.contains() method

You can also use operator.contains() method to check a string contains a substring.

Syntax: operator.contains(string,substring)

import operator s = "I love python programming." if operator.contains(s, "python"): print ("python is found.") else : print ("python is not found.")

Output:

python is found.

Originally published at https://www.pythonpip.com on November 7, 2021.

--

--

--

Hey, I am Parvez Alam. A software developer since 2009. I am a programming addict. I love learning and sharing knowledge. https://www.phpflow.com/

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Box Model

GitOps applied to Kubernetes environments

Why Outsource Software Development? 8 Reasons Why It Works

Alpha 6: The Next Step

Kotlin scope function mnemonics

How to Set up a Local Drupal Website With Lando

How the Fellowship produces and transitions expert technologists into the public sector

Something Happened on the Way to Analytics

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Parvez Alam

Parvez Alam

Hey, I am Parvez Alam. A software developer since 2009. I am a programming addict. I love learning and sharing knowledge. https://www.phpflow.com/

More from Medium

How to use Python frozenset type

Introducing the unit testing in Python

OOP Pythonic Ways(class vs attribute)

Error and Exception Handling in Python for Beginners