[Solved] Using sets in python to find common letters from two strings [closed]


Here you go:

@EDIT1: forgot that in python3.x print is a function.

@EDIT2: this was not part of the original request.

@EDIT3: formatted the output as later requested (I am not an expert in print formatting).

@EDIT4: modified the output to match the modified request

import string

def func(str1, str2):
    s1 = set(str1)
    s2 = set(str2)
    # requests are ambiguous "contained in both strings" can be either interpreted as intersections or union (i chose intersection)
    uni = s1.union(s2)
    letters_in_both_strings = [item for item in s1.intersection(s2) if item.isalpha()]
    non_letters_in_any_string = [item for item in uni if not item.isalpha()]
    all_letters = set(string.ascii_uppercase + string.ascii_lowercase)
    return letters_in_both_strings, all_letters.difference(uni), non_letters_in_any_string

str1 = input("Enter 1st string:")
str2 = input("Enter 2nd string:")
a, b, c = func(str1, str2)
print("|" + "".join(a) + "|\n|" + "".join(b) + "|\n|" + "".join(c) + "|\n")

0

solved Using sets in python to find common letters from two strings [closed]