In the previous article, we have discussed Python Program to Extract Only Characters from a Given String Show 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: Output: The Common Characters between the above given two Strings = goExample 2: Input: Given First String = have a nice Day Given Second String = A special dayOutput: The Common Characters between the above given two Strings = acdeiyProgram to Find Common Characters between Two StringsBelow are the ways to find common characters between two strings. Method #1: Using intersection Method (Static Input)Approach:
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 = goMethod #2 : Using intersection() Method (User Input)Approach:
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 = acdeiyExplore 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! Next Steps:
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. |