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.
  1. def count(str1, str2):
  2.     char_count = {}
  3.  
  4.    
  5.     for char in str1:
  6.         if char in char_count:
  7.             char_count[char] += 1
  8.    
  9.         else:
  10.             char_count[char] = 1
  11.  
  12.  
  13.     counter = 0
  14.  
  15.    
  16.     for char in str2:
  17.         if char in char_count and char_count[char] > 0:
  18.             counter += 1
  19.             char_count[char] -= 1
  20.  
  21.    
  22.     print("Total matching characters are: " + str(counter))
  23.  
  24. ret = False
  25.  
  26. while True:
  27.     print("\n================== Count the Matching Characters ==================\n\n")
  28.        
  29.     str1 = input("Enter 1st String: ")
  30.     str2 = input("Enter 2nd String: ")
  31.  
  32.  
  33.     count(str1, str2)
  34.  
  35.  
  36.     opt = input("\nDo you want to try again?(yes/no): ")
  37.  
  38.     if opt.lower() == 'yes':
  39.         ret=False
  40.     elif opt.lower() == 'no':
  41.         ret=True
  42.         print("Exiting program....")
  43.  
  44.     else:
  45.         print("Please enter yes/no:")
  46.         break
  47.  
  48.     if ret == False:
  49.         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

Python Tutorials

Add new comment