Find word in string Python
In this post, you will learn how to find the string using the Python programming language.
The string can have any number of characters in it. If a string contains a single quote within it, such a type of string can be written in double quotes. The backslash is used for escaping quotes and other special characters.
Python provides different ways to find words in a string. Here, we have mentioned most of them-
Python find word in string using the find() method
The find() method of Python finds the first occurrence of the specified value. It returns -1 if the value is not found.
string.find(value, start, end)
Here, value is a substring that needs to be searched in the given string, start is the starting position where the substring needs to be checked within the string, and end is the index of the last value for the specified range.
word = 'Every cloud has a silver lining'
print(word.find('silver'))
Output of the above code:
18
Python find word in string using the in operator
The easiest and fastest way to check whether a string contains a substring or not in Python is by using the "in" operator. This method returns True if a sequence with the specified value is present in the object.
str = 'Every cloud has a silver lining'
print("cloud" in str)
Output of the above code:
True
Python find word in string using __contains__() function
Python string __contains__() is an instance method and is used to check if a pattern or regex is contained within a given string. It returns a boolean value True or False depending on whether the string object contains the specified string object or not.
str = 'Every cloud has a silver lining'
word = "silver"
if (str.__contains__(word)):
print("String contains silver!")
else:
print("String does not contain silver!")
Output of the above code:
String contains silver!
Python find word in string using Regular Expression
Regular expression has an important role in computer science applications. Basically, it is useful in the search operation, where a user searches for a particular string that satisfies certain patterns. It provides patterns or a sequence of characters for matching text and defining search patterns.
Python provides the re (regular expression) module to achieve regular expression operations. This module is used for describing a search pattern or extracting information from text. It specifies the regular expression pattern in the first parameter and the target character string in the second parameter.
Syntax-re.search(pattern, string, flags[optional])
Example-
from re import search
str="Hello, World!"
substring = "tack"
if search("World", str):
print ("Substring Found!")
else:
print ("Not found!")
Output of the above code-
String contains cloud!
Related Articles
Convert Python list to numpy arrayConvert string to list Python
Python program to list even and odd numbers of a list
Python loop through list
Sort list in descending order Python
Convert array to list Python
Python take screenshot of specific window
Web scraping Python BeautifulSoup
Check if two strings are anagrams Python
Python program to add two numbers
Print new line python
Python for loop index
Convert List to Dataframe Python
numpy random choice
Dictionary inside list python
Check if list is empty Python
Python raise keyword
Python program to get the largest number from a list
Python program to map two lists into a dictionary