Python Check if Given String can be Formed by Concatenating String Elements of List (original) (raw)
Last Updated : 14 Feb, 2025
We are given a list of strings we need to check if given string can be formed by concatenating them. **For example, a = [“cat”, “dog”, “bird”] is given list and s = “catdog” is targe string we need to return True or False according to it, so that output should be False in this case.
Using a for
loop
A for loop iterates over each element in the list concatenating them to form a string result is checked against given string to determine if they are equal.
Python `
a = ["cat", "dog", "bird"] s = "catdog" con = ""
Concatenate list elements
for element in a: con += element
Check if the concatenated result equals the target
res = con == s
print(res)
`
**Explanation:
- Code iterates over each element in
a
appending it to the variablecon
to form a single string. - It checks if the concatenated string matches the target string
t
returningTrue
orFalse
.
Using join
.join method concatenates all elements in list into a single string resulting string is then compared to the target string to check for equality.
Python `
a = ["cat", "dog"] s = "catdog"
Join list elements and compare to the target
res = "".join(a) == s
print(res)
`
**Explanation:
join
method concatenates all elements in lista
into a single string without any separator.- Concatenated string is compared to target string
t
returningTrue
if they are equal andFalse
otherwise.
Using startswith
Using startswith we check if target string begins with each list element consuming matching part by adjusting the index accordingly. If all elements are matched sequentially and index reaches the target’s end string can be formed.
Python `
a = ["cat", "dog"] s = "catdog"
Initialize index and a flag
idx = 0 res = True
Iterate through the list and check each word
for word in a: # Check if the target substring matches the current word if s[idx:idx + len(word)] == word: idx += len(word) else: result = False break
Check if the entire target was consumed
res = res and idx == len(s)
print(res)
`
**Explanation:
- Code iterates through each word in list a and checks if the corresponding substring of t matches word.
- If all words match in sequence and fully consume the target t res is True otherwise it is set to False.
Similar Reads
- Concatenate all Elements of a List into a String - Python We are given a list of words and our task is to concatenate all the elements into a single string with spaces in between. For example, given the list: li = ['hello', 'geek', 'have', 'a', 'geeky', 'day'] after concatenation, the result will be: "hello geek have a geeky day". Using str.join()str.join( 3 min read
- Python | Remove List elements containing given String character Sometimes, while working with Python lists, we can have problem in which we need to perform the task of removing all the elements of list which contain at least one character of String. This can have application in day-day programming. Lets discuss certain ways in which this task can be performed. M 7 min read
- Python - Test if string contains element from list Testing if string contains an element from list is checking whether any of the individual items in a list appear within a given string. Using any() with a generator expressionany() is the most efficient way to check if any element from the list is present in the list. [GFGTABS] Python s = "Pyth 3 min read
- Check if any element in list satisfies a condition-Python The task of checking if any element in a list satisfies a condition involves iterating through the list and returning True if at least one element meets the condition otherwise, it returns False. For example, in a = [4, 5, 8, 9, 10, 17], checking ele > 10 returns True as 17 satisfies the conditio 3 min read
- Python Check if the List Contains Elements of another List The task of checking if a list contains elements of another list in Python involves verifying whether all elements from one list are present in another list. For example, checking if ["a", "b"] exists within ["a", "b", "c", "d"] would return True, while checking ["x", "y"] would return False. Using 3 min read
- Python - Check if string starts with any element in list We need to check if a given string starts with any element from a list of substrings. Let's discuss different methods to solve this problem. Using startswith() with tuplestartswith() method in Python can accept a tuple of strings to check if the string starts with any of them. This is one of the mos 3 min read
- Create List of Substrings from List of Strings in Python In Python, when we work with lists of words or phrases, we often need to break them into smaller pieces, called substrings. A substring is a contiguous sequence of characters within a string. Creating a new list of substrings from a list of strings can be a common task in various applications. In th 3 min read
- Python | Concatenate N consecutive elements in String list Sometimes, while working with data, we can have a problem in which we need to perform the concatenation of N consecutive Strings in a list of Strings. This can have many applications across domains. Let's discuss certain ways in which this task can be performed. Method #1: Using format() + zip() + i 8 min read
- Python | Check if all elements in list follow a condition Sometimes, while working with Python list, we can have a problem in which we need to check if all the elements in list abide to a particular condition. This can have application in filtering in web development domain. Let's discuss certain ways in which this task can be performed. Method #1 : Using 5 min read
- Python - String concatenation in Heterogeneous list Sometimes, while working with Python, we can come across a problem in which we require to find the concatenation of strings. This problem is easier to solve. But this can get complex cases we have a mixture of data types to go along with it. Let’s discuss certain ways in which this task can be perfo 4 min read