In this example, we are going to create a method which will do the followings:-

  1. Extract unique characters from two strings then group them into two separate lists.
  2. Create a new list consists of the characters in those two lists. The character within the list must only appear once and only consists of lowercase a-z characters.

Below is the solution.

  1. Create two lists with non-repeated characters from the given two strings.
  2. Loop through all the lowercase characters (from a-z) and if this character appears within any of those two lists then appends them to a new character list.
  3. Turn that new list into a string and then returns that new string.
import string
def longest(s1, s2):
    s1 = list(set(s1))
    s2 = list(set(s2))
    s3 = []

    for character in string.ascii_lowercase:
        if character in s1 or character in s2:
    return ''.join(s3)

We will use the string module ascii_lowercase list property to save all the typing we need in order to create the lowercase letters list.

Homework :

Create a new string which only consists of non-repeated digits in the ascending order from two given strings. For example, s1 = “agy569” and s2 = “gyou5370” will produce s3 = “035679”. Write your solution in the comment box below this article.

Do you finish the homework all by your own?

Please follow and like us:

1 Comment

  1. You can use set operations to solve this as a one-liner:

    def solve(s1, s2, whitelist):
    return ”.join(sorted((set(s1) | set(s2)) & set(whitelist)))

Leave a Reply

Your email address will not be published. Required fields are marked *