Skip to content
Related Articles

Related Articles

Python | Extract characters except of K string

Improve Article
Save Article
  • Last Updated : 08 Sep, 2022
Improve Article
Save Article

Sometimes, while working with Python strings, we can have a problem in which we require to extract all the elements of string except those which present in a substring. This is quite common problem and has application in many domains including those of day-day and competitive programming. Lets discuss certain ways in which this task can be performed. 

Method #1: Using loop This is brute force approach to this problem. In this we employ not operator to test for element presence in master string and extract it if the element is not present in K string. 

Python3




# Python3 code to demonstrate working of
# Extract characters except of K string
# Using loop
 
# initializing strings
test_str1 = "geeksforgeeks is best"
test_str2 = "fes"
 
# printing original strings
print("The original string 1 is : " + test_str1)
print("The original string 2 is : " + test_str2)
 
# Extract characters except of K string
# Using loop
res = []
for ele in test_str1:
    if ele not in test_str2:
        res.append(ele)
res = ''.join(res)
 
# printing result
print("String after removal of substring elements : " + str(res))

Output : 

The original string 1 is : geeksforgeeks is best
The original string 2 is : fes
String after removal of substring elements : gkorgk i bt

Method #2: Using set operations This task can also be performed by including set operations. One can perform a set difference to get the difference of elements. The drawbacks are that order is not preserved and duplicates are removed. 

Python3




# Python3 code to demonstrate working of
# Extract characters except of K string
# Using set operations
 
# initializing strings
test_str1 = "geeksforgeeks is best"
test_str2 = "fes"
 
# printing original strings
print("The original string 1 is : " + test_str1)
print("The original string 2 is : " + test_str2)
 
# Extract characters except of K string
# Using set operations
res = ''.join(list(set(test_str1) - set(test_str2)))
 
# printing result
print("String after removal of substring elements : " + str(res))

Output : 

The original string 1 is : geeksforgeeks is best
The original string 2 is : fes
String after removal of substring elements : oti krbg

Method#3: Using re.sub() : This task can also be performed using re.sub function.  re.sub() function is used to substitute the matched pattern with the another string. We can use empty string for substituting which work as we erase all the matched character in string. 

Python3




# # Python3 code to demonstrate working of
# Extract characters except of K string
# Using re.sub
import re
# initializing strings
test_str1 = "geeksforgeeks is best"
test_str2 = "fes"
 
# printing original strings
print("The original string 1 is : " + test_str1)
print("The original string 2 is : " + test_str2)
 
# Extract characters except of K string
# Using re.sub
res = re.sub(f'[{test_str2}]', "", test_str1)
 
# printing result
print("String after removal of substring elements : " + str(res))

Output

The original string 1 is : geeksforgeeks is best
The original string 2 is : fes
String after removal of substring elements : gkorgk i bt

Method #4 : Using replace() method

Python3




# Python3 code to demonstrate working of
# Extract characters except of K string
# Using loop and replace()
 
# initializing strings
test_str1 = "geeksforgeeks is best"
test_str2 = "fes"
 
# printing original strings
print("The original string 1 is : " + test_str1)
print("The original string 2 is : " + test_str2)
 
# Extract characters except of K string
# Using loop and replace()
for i in test_str2:
    test_str1=test_str1.replace(i,"")
 
# printing result
print("String after removal of substring elements : " + str(test_str1))

Output

The original string 1 is : geeksforgeeks is best
The original string 2 is : fes
String after removal of substring elements : gkorgk i bt

Time Complexity: O(n)

Auxiliary Space: O(n)


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!