Python - Test String in Character List and vice-versa
Last Updated :
26 Apr, 2023
Given a String, check if it's present in order in the character list and vice versa.
Input : test_str = 'geeks', K = ['g', 'e', 'e', 'k', 'f', 'o', 'r', 'g', 'e', 'e', 'k', 's'] [ String in Character list ]
Output : True
Explanation : geeks is present in list , starting from 7th index till end.
Input : test_str = 'geeksforgeeks', K = ['g', 'e', 'e', 'k', 's'] [Character list in String]
Output : True
Explanation : ['g', 'e', 'e', 'k', 's'] present in string, starting from the beginning of string.
Method #1: Using in operator + join() [ String in character list ]
In this, we convert the character list to a string using join() and apply it in the operator to test for substring presence.
Python3
# Python3 code to demonstrate working of
# Test String in Character List and vice-versa
# Using in operator and join() [String in list]
# initializing string
test_str = 'geeks'
# printing original string
print("The original string is : " + str(test_str))
# initializing Character list
K = ['g', 'e', 'e', 'k', 'f', 'o', 'r', 'g', 'e', 'e', 'k', 's']
# joining list
joined_list = ''.join(K)
# checking for presence
res = test_str in joined_list
# printing result
print("Is String present in character list : " + str(res))
OutputThe original string is : geeks
Is String present in character list : True
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #2 : Using in operator + join() [ Character List in String ]
In this, the target character list is converted to String and then checked in String using in operator.
Python3
# Python3 code to demonstrate working of
# Test String in Character List and vice-versa
# Using in operator + join() [ Character List in String ]
# initializing string
test_str = 'geeksforgeeks'
# printing original string
print("The original string is : " + str(test_str))
# initializing Character list
K = ['g', 'e', 'e', 'k', 's']
# joining list
joined_list = ''.join(K)
# checking for presence
res = joined_list in test_str
# printing result
print("Is character list present in String : " + str(res))
OutputThe original string is : geeksforgeeks
Is character list present in String : True
Time Complexity: O(n) -> as join method takes O(n) complexity
Auxiliary Space: O(n)
Method 3 : using the set() function and intersection() method
Step-by-step approach:
- Initialize the original string and print it.
- Initialize the character list.
- Convert the character list to a set using the set() function.
- Get the intersection of the character set and the set of the original string using the intersection() method.
- Check if the length of the intersection set is equal to the length of the character set.
- Print the result.
Python3
# Python3 code to demonstrate working of
# Test String in Character List and vice-versa
# Using set() and intersection() [ Character List in String ]
# initializing string
test_str = 'geeksforgeeks'
# printing original string
print("The original string is : " + str(test_str))
# initializing Character list
K = ['g', 'e', 'e', 'k', 's']
# converting list to set
char_set = set(K)
# getting intersection of sets
intersect_set = char_set.intersection(set(test_str))
# checking if all characters are present
res = len(intersect_set) == len(char_set)
# printing result
print("Is character list present in String : " + str(res))
OutputThe original string is : geeksforgeeks
Is character list present in String : True
Time complexity: O(n), where n is the length of the original string.
Auxiliary space: O(k), where k is the length of the character list.
Similar Reads
Python - Strings with all given List characters GIven Strings List and character list, extract all strings, having all characters from character list. Input : test_list = ["Geeks", "Gfg", "Geeksforgeeks", "free"], chr_list = [ 'f', 'r', 'e'] Output : ['free', "Geeksforgeeks"] Explanation : Only "free" and "Geeksforgeeks" contains all 'f', 'r' and
4 min read
Python - Test if Kth character is digit in String Given a String, check if Kth index is a digit. Input : test_str = 'geeks9geeks', K = 5 Output : True Explanation : 5th idx element is 9, a digit, hence True.Input : test_str = 'geeks9geeks', K = 4 Output : False Explanation : 4th idx element is s, not a digit, hence False. Method #1: Using in operat
5 min read
Python - Fill list characters in String Given String and list, construct a string with only list values filled. Input : test_str = "geeksforgeeks", fill_list = ['g', 's', 'f', k] Output : g__ksf__g__ks Explanation : All occurrences are filled in their position of g, s, f and k. Input : test_str = "geeksforgeeks", fill_list = ['g', 's'] Ou
9 min read
Python - Convert String to unicode characters Convert String to Unicode characters means transforming a string into its corresponding Unicode representations. Unicode is a standard for encoding characters, assigning a unique code point to every character.For example:string "A" has the Unicode code point U+0041.string "ä½ å¥½" corresponds to U+4F60
2 min read
Splitting String to List of Characters - Python We are given a string, and our task is to split it into a list where each element is an individual character. For example, if the input string is "hello", the output should be ['h', 'e', 'l', 'l', 'o']. Let's discuss various ways to do this in Python.Using list()The simplest way to split a string in
2 min read