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
- Python | Extract only characters from given string
- Python - Extract Upper Case Characters
- Python | Extract digits from given string
- Python | Extract numbers from string
- Python | Extract words from given string
- Python | Extract Score list of String
- Python | Extract odd length words in String
- Python | Extract Numbers in Brackets in String
- Python | Extract length of longest string in list
- Python Regex to extract maximum numeric value from a string
- Python - Remove front K characters from each string in String List
- Python - Key with all Characters in String
- Iterate over characters of a string in Python
- Python | Get positional characters from String
- Python - String with most unique characters
- Python - String uncommon characters
- Python | Ways to check string contain all same characters
- Python | Check if frequencies of all characters of a string are different
- Python | Splitting string to list of characters
- Concatenated string with uncommon characters in Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.