Python | Extract characters except of K string
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.
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 difference of elements. The drawbacks are that order is not preserved and duplicates are removed.
The original string 1 is : geeksforgeeks is best The original string 2 is : fes String after removal of substring elements : oti krbg
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course