Python Programs | IT Developer
IT Developer

Python Programs



Share with a Friend

Python Programs - String

Find common characters between two strings - Python Program

Example 1 :

s1 = "python" s2 = "typhoon" common = set(s1) & set(s2) print("".join(common))

Output

 
OUTPUT  :
thony

Example 2 :

def find_common_characters(string1, string2): """ Finds and returns a set of common characters between two strings. Args: string1 (str): The first input string. string2 (str): The second input string. Returns: set: A set containing the common characters. """ set1 = set(string1) # Convert the first string to a set of its characters set2 = set(string2) # Convert the second string to a set of its characters common_chars = set1.intersection(set2) # Find the intersection of the two sets return common_chars # Get input from the user str1 = input("Enter the first string: ") str2 = input("Enter the second string: ") # Find the common characters common = find_common_characters(str1, str2) # Print the result if common: print(f"The common characters are: {', '.join(sorted(list(common)))}") else: print("There are no common characters between the two strings.")

Output

 
OUTPUT  :
Enter the first string: typhoon
Enter the second string: python
The common characters are: h, n, o, p, t, y