
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Check if given string can be formed by concatenating string elements of list in Python
We sometimes need to check if a required string can be formed from many number of strings that are present in a list. It also should not matter in what order the strings are present in the list which have to be joined to get the required string.
With permutations
From itertools we can use the permutations function which will give us the possible combinations of the strings in the list in various order. As soon as a given combination matches the required string, we conclude that the string can be formed.
Example
from itertools import permutations chk_str = 'balloon' Alist = ['fly','on', 'o', 'hot', 'ball', 'air'] def findstring(strchk, biglist): for i in range(2, len(biglist) + 1): for perm in permutations(biglist, i): if ''.join(perm) == strchk: return True return False # Using the function if(findstring(chk_str,Alist)): print("String can be formed.") else: print("String can not be formed.")
Output
Running the above code gives us the following result −
String can be formed.
With Regular Expressions
The re module provides the compile function which will create the possible strings by specifying the regular expression pattern. Then it will be compared with the string to be checked. If the result is not none then we can conclude the string can be formed.
Example
import re chk_str = 'balloon' Alist = ['fly','on', 'o', 'hot', 'ball', 'air'] def findstring(strchk, biglist): r = re.compile("(?:" + "|".join(biglist) + ")*$") if r.match(strchk) != None: return True return False # Using the function if(findstring(chk_str,Alist)): print("String can be formed.") else: print("String can not be formed.")
Output
Running the above code gives us the following result −
String can be formed.
- Related Articles
- Check whether given string can be generated after concatenating given strings in Python
- Check if a string can be formed from another string using given constraints in Python
- Check whether second string can be formed from characters of first string in Python
- Check if a string can be obtained by rotating another string 2 places in Python
- Check if given string can be split into four distinct strings in Python
- Check if a string can become empty by recursively deleting a given sub-string in Python
- Check if a two-character string can be made using given words in Python
- Check if a string can be repeated to make another string in Python
- Check if a string can be converted to another string by replacing vowels and consonants in Python
- Python Check if suffix matches with any string in given list?
- Check if characters of a given string can be rearranged to form a palindrome in Python
- Program to check a string can be broken into given list of words or not in python
- Check if elements of array can be made equal by multiplying given prime numbers in Python
- Check if a string can become empty by recursively deleting a given sub-string in C++
- Check if characters of one string can be swapped to form other in Python
