How to Find Common Characters in Two Strings Using Python
In this tutorial, we’ll learn how to program "How to Find Common Characters in Two Strings Using Python." The objective is to display the common letters from both strings. This tutorial will guide you through the process step by step on how to identify the common characters in strings. By the end, you’ll have a clear understanding of how to efficiently perform this task in Python.
This topic is straightforward to understand. Just follow the instructions I provide, and you’ll complete it with ease. The program I’ll demonstrate will show you the correct and efficient way to display all common letters. So, let’s dive into the coding process!
Getting Started:
First you will have to download & install the Python IDLE's, here's the link for the Integrated Development And Learning Environment for Python https://www.python.org/downloads/.
Creating Main Function
This is the main function of the application. The following code will display a simple GUI in terminal console that will display program. To do this, simply copy and paste these blocks of code into the IDLE text editor.- while True:
- print("\n============= Find Common Characters in Two Strings =============\n")
- s1=input("Enter first string: ")
- s2=input("Enter second string: ")
- a=list(set(s1)&set(s2))
- print("The common letters are:")
- for i in a:
- print(i)
- opt = input("\nDo you want to try again?(yes/no): ")
- if opt.lower() == 'yes':
- ret=False
- elif opt.lower() == 'no':
- ret=True
- print("Exiting program....")
- else:
- print("Please enter yes/no:")
- break
- if ret == False:
- continue
This Python program is designed to find and display the common characters between two user-provided strings. Inside an infinite loop, it prompts the user to enter two strings, then converts each string into a set of unique characters. Using set intersection (set(s1) & set(s2)), it determines which characters appear in both strings.
The result is converted into a list and each common character is printed individually. After displaying the results, the program asks the user if they want to try again. If the user answers "yes", the process repeats; if "no", the program exits gracefully. This simple yet effective approach demonstrates how Python's built-in set operations can be used for character-level comparison between strings.
Output:

There you have it we successfully created How to Find Common Characters in Two Strings Using Python. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!
More Tutorials for Python Language
Add new comment
- 9 views