• Home
  • Insurance
  • Banking
  • Loans
  • Remitance
  • About us
Facebook Twitter Instagram
  • links
Biz Assurance
Subscribe
  • Home
  • Insurance
  • Banking
  • Loans
  • Remitance
  • About us
Biz Assurance
Home»Tech»Python Check If String Contains Substring From List
Tech

Python Check If String Contains Substring From List

Alicia CormieBy Alicia CormieNo Comments3 Mins Read
Facebook Twitter WhatsApp
Share
Facebook Twitter LinkedIn WhatsApp

In Python, the strings are the combination of characters contained in quotes. Like strings, lists are also used to save a collection of strings. While working with Python, users frequently encounter situations where they must determine if the provided string contains a substring from any given Python list. To resolve this encountered scenario, multiple methods are used.

This guide will provide different techniques to check if a string contains a substring from the list in Python.

How to Check If String Contains Substring From List in Python?

To check if the string has a substring from the list in Python, the following techniques are used:

  • List Comprehension
  • any()
  • “for” Loop

Method 1: Check If String Contains Substring From List in Python through List Comprehension

To check if the list contains the substring from the Python list, the list comprehension can be used. The implementation of the list comprehension is stated in the below example.

Example

First, create a string variable and pass a string. Then, define and initialize a list:

first_str = “LinuxHint is the world best tutorial website”

var_list = [‘forum’ , ‘website’]

Now, call the “print()” function to view the input value of string and list:

print(“My input string is : “ + first_str)

print(“My initialized list is : “ + str(var_list))

Use the “for” loop and check the condition with “if” statement from the input string:

resultant_str = [x for x in var_list if(x in first_str)]

Now, view the checked condition use a “bool()” method inside the “print()” statement:

print(“Is my string contain a list element? “ + str(bool(resultant_str)))

According to the below given output, the provided string contains the substring from the initialized Python list:

Method 2: Check If String Contains Substring From List in Python Using “any()” Method

Another way to check if the string contains a substring from the Python list or not is the “any()” method. This method checks each list element to an input string match.

Example

Call the “any()” method along with the “for” loop to check the list variable and store it into the “resultant_str” variable:

resultant_str = any(x in first_str for x in var_list)

Call the “print()” function to display the filtered result:

print(“Is my string contain a list element? “ + str(resultant_str))

Output

Method 3: Check If String Contains Substring From List in Python Using “for” Loop

We can also use the “for” loop for checking if the provided string contains a substring from the list in Python or not. The “for” loop is the iterative function that iterates over the provided sequence of the string element.

Example

Use the “for” loop to check the substring by utilizing the “if” condition, and print the desired result through the print statement:

for substring in var_list:

if substring in first_str:

print(“String contain a list element”)

break

It can be observed the provided string contains the substring from the previously initialized Python list:

That’s all! You have learned about the different techniques to view if a string has a substring from the list in Python.

Conclusion

To check if the provided string has the substring from the provided list in Python, the “list comprehension”, the “any()” method, and the iterative function “for” loop are used. All techniques first check the provided condition and then display the results. This guide illustrated multiple ways to check if a string contains a substring from the list in Python.

Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
Previous ArticleConvert 1d Array to 2d Array Python
Next Article Don Toliver’s ‘Love Sick’: Everything To Know Including Its Release Date, Tracklist, And More

Related Posts

How to Fix “Not connected – No connections are available” Error in Windows

How to Fix “Blurry Font Problem” in Windows 10

How to Fix “Can’t create new folder” in Windows 10

Add A Comment

Leave A Reply Cancel Reply

Grimes Explained Why She’s ‘Purposefully’ Trying To Make Herself ‘Infinitely Less Accessible’

It Appears That Netflix May Have Edited One Of Chris Rock’s Jokes About Will Smith In His Comedy Special

There’s A Rumor That Cardi B And Megan Thee Stallion Want To Remake A Cult-Classic Halle Berry Movie And Cardi Addressed It

The Biggest Takeaways From ‘Bel-Air’ Season 2, Episode 3

  • Homepage
  • Sitemap
© 2023 Biz Assurance - Designed by Curtiex Ventures.

Type above and press Enter to search. Press Esc to cancel.