How to Count the Matching Characters in Python
In this tutorial, we will program 'How to Count the Matching Characters in Python.' We will learn how to count the total matching characters. The objective is to teach you how to easily identify the possible matching characters. I will provide a sample program to demonstrate the actual coding process in this tutorial.
This topic is very easy to understand. Just follow the instructions I provide, and you will be able to do it yourself with ease. The program I will show you covers the basics of programming to count the matching characters. I will do my best to provide you with a simple method for counting the characters. So, let's start with the coding.
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.- def count(str1, str2):
- char_count = {}
- for char in str1:
- if char in char_count:
- char_count[char] += 1
- else:
- char_count[char] = 1
- counter = 0
- for char in str2:
- if char in char_count and char_count[char] > 0:
- counter += 1
- char_count[char] -= 1
- print("Total matching characters are: " + str(counter))
- ret = False
- while True:
- print("\n================== Count the Matching Characters ==================\n\n")
- str1 = input("Enter 1st String: ")
- str2 = input("Enter 2nd String: ")
- count(str1, str2)
- 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 script counts the number of matching characters between two user-provided strings. It repeatedly prompts the user to input two strings, counts the matching characters using a function, and displays the result. After each count, the user is asked if they want to try again, allowing them to either continue or exit the program.
Output:
There you have it we successfully created How to Count the Matching Characters in 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
- 182 views