Python String contains() method Tutorial
Hello in this tutorial, we will understand Python String contains() method.
1. Introduction
The contains in python is used to check whether a string contains a substring or not. A substring is the sequence of characters within a string. We will explore the below methods to check if a string contains another string or not.
- The
find()
method checks if a substring is part of the string or not. If the string uses the given substring the method returns the starting index of the substring else it will return -1. It is represented by the following syntax i.e.string.find(substring)
- The
index()
method checks if a substring is part of the string or not. If found it returns the starting index of the first occurrence of a substring and if not it generates aValueError
exception which can be handled with the help of the try-expect-else block in python. It is represented by the following syntax i.e.string.index(substring)
- The
count()
method checks the occurrence of a substring in the string. If found it returns 1 otherwise 0. It is represented by the following syntax i.e.string.count(substring)
- The
replace()
method is used to replace the occurrences of a substring with a new string. It is represented by the following syntax i.e.string.replace(old_word, new_word, no_of_occurences)
whereno_of_occurences
is an optional field that denotes the number of times you want to replace the old substring with the new substring - The
in
operator checks if the substring is a part of the string or not. If present it returns true otherwise false. It is represented by the following syntax i.e.substring in string
- Using the regular expression to check if the substring is a part of the string or not through the pattern matching. For this, we will use a python in-built module known as
re
. This module contains a method known assearch
which you can use to match a substring pattern - We will use also use another python in-built module known as
operator
. This module contains a method known ascontains()
which you can use to check the presence of a substring in a string
Let us see these different ways in action.
1.1 Setting up Python
If someone needs to go through the Python installation on Windows, please watch this link. You can download the Python from this link.
2. Python String contains() Tutorial
I am using JetBrains PyCharm as my preferred IDE. You are free to choose the IDE of your choice.
2.1 Python String contains() Tutorial
Let us understand the different ways through programming.
Python String contains()
# python string contain() method tutorial # import statement import operator # import statement from re import search # approach 1 - index() def index(string, search_key): try: string.index(search_key) print('Found.') except ValueError: print('Search key not found.') # approach 2 - find() def find(string, search_key): if string.find(search_key) != -1: print('Found.') else: print('Search key not found.') # approach 3 - count() def count(string, search_key_1, search_key_2): x = string.count(search_key_1) print('Is search key 1 = {} present in the given string = {}'.format(search_key_1, x)) y = string.count(search_key_2) print('Is search key 2 = {} present in the given string = {}'.format(search_key_2, y)) # approach 4 - replace() def replace(string, old_word, new_word, no_of_occurrences): z = string.replace(old_word, new_word, no_of_occurrences) print('Replaced string = {}'.format(z)) # approach 5 - in operator def in_operator(string, search_key): if search_key in string: print('Found.') else: print('Search key not found.') # approach 6 - regex expression def regex(string, search_key): if search(search_key, string): print('Found.') else: print('Search key not found.') # approach 7 - using operator module def operator_mod(string, search_key): if operator.contains(string, search_key): print('Found.') else: print('Search key not found.') # main() method def main(): string = 'What am I even tripping for? Everything’s gonna work out exactly the way it’s supposed to' print('\n--- Approach 1 ---\n') index(string, 'tripping') print('\n--- Approach 2 ---\n') find(string, 'supposed') print('\n--- Approach 3 ---\n') count(string, 'tripping', 'done') print('\n--- Approach 4 ---\n') no_of_occurrences = 1 replace(string, 'tripping', 'TRIPPING', no_of_occurrences) print('\n--- Approach 5 ---\n') in_operator(string, 'going') print('\n--- Approach 6 ---\n') regex(string, 'geek') print('\n--- Approach 7 ---\n') operator_mod(string, 'gonna') # driver code if __name__ == '__main__': main()
If everything goes well the following output will be shown in the IDE console.
Console Output
--- Approach 1 --- Found. --- Approach 2 --- Found. --- Approach 3 --- Is search key 1 = tripping present in the given string = 1 Is search key 2 = done present in the given string = 0 --- Approach 4 --- Replaced string = What am I even TRIPPING for? Everything’s gonna work out exactly the way it’s supposed to --- Approach 5 --- Search key not found. --- Approach 6 --- Search key not found. --- Approach 7 --- Found.
That is all for this tutorial and I hope the article served you with whatever you were looking for. Happy Learning and do not forget to share!
3. Summary
In this tutorial, we learned:
- contains() method in python programming to find the presence of a substring in a string
- Sample program to understand the different use cases
You can download the source code of this tutorial from the Downloads section.
4. Download the Project
This was a python programming tutorial to understand the different use cases for finding the presence of a substring in a string.
You can download the full source code of this example here: Python String contains() method Tutorial
Last updated on Mar. 2nd, 2022