How to Check If String Contains Word in Python?

AuthorSumit Dey Sarkar

Pubish Date21 May 2023

categoryPython

In this tutorial we will learn How to Check If String Contains Word in Python?

 

https://www.teknowize.com/attachments/file_1684690198.png

 

Check if a word is in a string in Python

Do you want to know whether a string includes the specific word? In this tutorial we will learn various methods and techniques to check this efficiently and effectively using Python.

 

Method 1: Using the in Operator

Using Python's in operator is one of the easiest and most direct ways to find out if a string includes a word. This operator allows us to check if a substring or word exists within a larger string. By leveraging this operator, we can easily perform the desired task with minimal code.

string = "Hello, world! Welcome to Python."
word = "world"

if word in string:
    print("The string contains the word.")
else:
    print("The string does not contain the word.")

A string variable called string and a word variable called word are defined in the preceding example. By using the in operator, we check if word is present in string and print the appropriate message. This method provides a quick and efficient solution for determining the presence of a word within a string.

 

Method 2: Using Regular Expressions

If you need more advanced pattern matching capabilities or want to perform more complex searches, regular expressions can be a powerful tool. Python provides a built-in module called re that allows us to work with regular expressions easily.

import re

string = "Hello, world! Welcome to the Python."
word = "world"

if re.search(r"\b" + re.escape(word) + r"\b", string):
    print("The string contains word.")
else:
    print("The string does not contain word.")

This method involves importing the re module and searching the string for a pattern using the re.search() function. By utilizing the \b (word boundary) metacharacter and the re.escape() function, we ensure that we match the exact word rather than a substring.

 

Method 3: Splitting and Searching

Another method to identify if a string contains a word is by splitting it up into separate words and then look for the target word in the list that results. When you need to carry out further actions on the words in the string, this method is really helpful.

string = "Hello, world! Welcome to Python."
word = "world"

words = string.split()

if word in words:
    print("The string contains the word.")
else:
    print("The string does not contain the word.")

In the aboveexample, the string is divided into a list of words using the split() method. We then check if the word exists in the resulting list. This method provides flexibility and allows for further manipulation of individual words if needed.

 

Method 4: Using List Comprehension

With just one line of code, we can generate lists using Python's succinct yet potent list comprehension functionality. By utilizing list comprehension, we can check if a string contains a word while maintaining code readability and brevity.

string = "Hello, world! Welcome to Python."
word = "world"

contains_word = any(word in s for s in string.split())

if contains_word:
    print("The string contains the word.")
else:
    print("The string does not contain the word.")

In this approach, we combine the any() function with list comprehension to check if any word in the string matches the desired word. This method is concise, efficient, and provides a clear indication of whether the word exists in the string.

 

Conclusion

In this you have learned multiple way (method) to check if a string contains a word in Python. We explored the usage of the in operator, regular expressions, splitting and searching, and list comprehension. Each method offers its own advantages and can be chosen based on your specific requirements.

 

Remember to take into account the difficulty of your search, the necessity for pattern matching, and any additional actions you might take on the words in the string. By utilizing these techniques, you can efficiently determine if a string contains a word in Python, enhancing your programming capabilities.

 

Comments 0

Leave a comment