Find common characters in list of strings python

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.

See also  PHP code snippet - How to change directory in command processor?

This is a Python Program to check common letters in the two input strings.

The program takes two strings and checks common letters in both the strings.

1. Enter two input strings and store it in separate variables. 2. Convert both of the strings into sets and find the common letters between both the sets. 3. Store the common letters in a list. 4. Use a for loop to print the letters of the list.

5. Exit.

Here is source code of the Python Program to check common letters in the two input strings. The program output is also shown below.

s1=raw_input("Enter first string:") s2=raw_input("Enter second string:") a=list(set(s1)&set(s2)) print("The common letters are:") for i in a: print(i)

1. User must enter two input strings and store it in separate variables. 2. Both of the strings are converted into sets and the common letters between both the sets are found using the ‘&’ operator. 3. These common letters are stored in a list.

4. A for loop is used to print the letters of the list.

Note: Join free Sanfoundry classes at Telegram or Youtube

  Case 1: Enter first string:Hello Enter second string:How are you The common letters are: H e o   Case 2: Enter first string:Test string Enter second string:checking The common letters are: i e g n

Sanfoundry Global Education & Learning Series – Python Programs.

To practice all Python programs, here is complete set of 150+ Python Problems and Solutions.

Take Python Programming Tests Now!

  • Get Free Certificate of Merit in Python Programming
  • Participate in Python Programming Certification Contest
  • Become a Top Ranker in Python Programming
  • Take Python Programming Tests
  • Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
  • Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

Find common characters in list of strings python

Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.