Introduction
Sets are a powerful tool in Python that can be used to find common letters between two strings. Sets are unordered collections of unique elements, meaning that each element can only appear once in a set. This makes them ideal for finding common letters between two strings, as each letter can only appear once in the set. In this article, we will discuss how to use sets in Python to find common letters from two strings.
Solution
# Create two sets from the two strings
set1 = set(string1)
set2 = set(string2)
# Find the common letters
common_letters = set1 & set2
# Print the common letters
print(common_letters)
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]
Sets are a powerful tool in Python that can be used to find common letters from two strings. Sets are unordered collections of unique elements, meaning that each element can only appear once in a set. This makes them ideal for finding common letters between two strings, as each letter can only appear once in the set.
To find common letters between two strings, we can first create two sets from the strings. We can do this by using the set() function, which takes a string as an argument and returns a set of all the unique characters in the string. For example, if we have two strings, “Hello” and “World”, we can create two sets like this:
string1 = "Hello"
string2 = "World"
set1 = set(string1)
set2 = set(string2)
Now that we have two sets, we can use the intersection() method to find the common letters between the two strings. The intersection() method takes two sets as arguments and returns a set of all the elements that appear in both sets. For example, if we use the sets we created above, the intersection() method will return a set containing the letter “l”:
common_letters = set1.intersection(set2)
# common_letters is now a set containing the letter "l"
Using sets in Python is a great way to quickly and easily find common letters between two strings. It is also a useful tool for other tasks, such as finding unique elements in a list or removing duplicates from a list.