Program to Find Common Characters between Two Strings

Python Program to Find Common Characters between Two Strings

In the previous article, we have discussed Python Program to Extract Only Characters from a Given String
Given two strings, the task is to find the common characters between Two Strings.

In this case, we use some of the built-in functions like join(), lower(), sorted() and intersection() methods.

join() :

The string method join()  takes all of the items in an iterable and returns a string after joining them all together. Iterable types include list, tuple, string, dictionary, and set.

lower() :

As the name indicates, It takes a string and converts all uppercase characters to lowercase before returning it.

sorted method() :

The sorted() method is used to orderly sort the elements of a sequence (either ascending or descending).

intersection() method :

The intersection() method is used to find all the elements that are shared by two given sets.

Examples:

Example 1:

Input:

Given First String = Hello Btechgeeks
Given Second String = good morning

Output:

The Common Characters between the above given two Strings =   go

Example 2:

Input:

Given First String = have a nice Day
Given Second String = A special day

Output:

The Common Characters between the above given two Strings = acdeiy

Program to Find Common Characters between Two Strings

Below are the ways to find common characters between two strings.

Method #1: Using intersection Method (Static Input)

Approach:

  • Give the first string as static input, convert the given string into the lower case using the built-in lower() method and store it in a variable.
  • Give the second string as static input, convert the given string into the lower case using the built-in lower() method and store it in another variable.
  • Get the Common characters between both the above-given strings using the built-in intersection() method which is a set method.
  • Sort the above-given string using the built-in sorted() method.
  • Join the above-given string using the built-in join()method.
  • Print all the Common Characters between the above given two Strings.
  • The Exit of the program.

Below is the implementation:

# Give the first string as static input  , convert the given string into lower case
# using built-in lower() method and store it in a variable.
fst_strng = "Hello Btechgeeks".lower()
# Give the  second string as static input , convert the given string into lower case
# using built-in lower() method and store it in another variable.
secnd_strng = "good morning".lower()
# Get the Common characters between both the above given strings using built-in
# intersection() method which is a set method.
# Sort the above given string using  built-in sorted() method.
# Join the the above given string using built-in join()method .
# Print all the Common Characters between the above given two Strings.
print("The Common Characters between the above given two Strings = ",
      ''.join(sorted(set.intersection(set(fst_strng), set(secnd_strng)))))

Output:

The Common Characters between the above given two Strings =   go

Method #2 : Using intersection() Method (User Input)

Approach:

  • Give the first string as User input using the input() function, convert the given string into the lower case using the built-in lower() method and store it in a variable.
  • Give the second string as User input using the input() function, convert the given string into the lower case using the built-in lower() method, and store it in another variable.
  • Get the Common characters between both the above-given strings using the built-in intersection() method which is a set method.
  • Sort the above-given string using the built-in sorted() method.
  • Join the above-given string using the built-in join()method.
  • Print all the Common Characters between the above given two Strings.
  • The Exit of the program.

Below is the implementation:

# Give the first string as User input  using the input() function , convert the given string into lower case
# using built-in lower() method and store it in a variable.
fst_strng = input("Enter some Random String = ").lower()
# Give the  second string as User input  using the input() function, convert the given string into lower case
# using built-in lower() method and store it in another variable.
secnd_strng = input("Enter some Random String = ").lower()
# Get the Common characters between both the above given strings using built-in
# intersection() method which is a set method.
# Sort the above given string using  built-in sorted() method.
# Join the the above given string using built-in join()method .
# Print all the Common Characters between the above given two Strings.
print("The Common Characters between the above given two Strings = ",
      ''.join(sorted(set.intersection(set(fst_strng), set(secnd_strng)))))

Output:

Enter some Random String = have a nice Day
Enter some Random String = A special day
The Common Characters between the above given two Strings = acdeiy

Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.