Find Mirror Characters in a String Using a Dictionary in Python

In this tutorial, we will learn how to program "Find Mirror Characters in a String Using a Dictionary in Python." The objective is to find mirror characters in a string. This tutorial will guide you through the process step by step, showing you how to identify mirror characters. By the end, you will have a clear understanding of how to efficiently complete this task in Python.

This topic is straightforward to understand. Just follow the instructions I provide, and you will complete it with ease. The program I will demonstrate will show you the correct and efficient way to get the mirror characters in a string. 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.
  1. def mirrorChars(input,k):
  2.         original = 'abcdefghijklmnopqrstuvwxyz'
  3.         reverse = 'zyxwvutsrqponmlkjihgfedcba'
  4.         dictChars = dict(zip(original,reverse))
  5.  
  6.  
  7.         prefix = input[0:k-1]
  8.         suffix = input[k-1:]
  9.         mirror = ''
  10.  
  11.        
  12.         for i in range(0,len(suffix)):
  13.                 mirror = mirror + dictChars[suffix[i]]
  14.  
  15.        
  16.         print (prefix+mirror)
  17.                
  18.  
  19. if __name__ == "__main__":
  20.         while True:
  21.                 print("\n============ Find Mirror Characters in a String Using a Dictionary ============\n")
  22.                 my_string = input("Enter a string: ")
  23.                 i = 3
  24.                 mirrorChars(my_string,i)
  25.  
  26.                 opt = input("\nDo you want to try again?(yes/no): ")
  27.        
  28.                 if opt.lower() == 'yes':
  29.                         ret=False
  30.                 elif opt.lower() == 'no':
  31.                         ret=True
  32.                         print("Exiting program....")
  33.                 else:
  34.                         print("Please enter yes/no:")
  35.                         break
  36.  
  37.                 if ret == False:
  38.                         continue

This Python program finds the mirror characters in a string from a given position using a dictionary that maps each lowercase letter to its opposite letter in the alphabet (e.g., 'a' to 'z', 'b' to 'y', etc.). It keeps the prefix of the string unchanged up to the specified index k-1, and replaces each character in the suffix (from index k-1 onward) with its mirror character, then prints the combined result.

Output:

There you have it we successfully created Find Mirror Characters in a String Using a Dictionary 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