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.- def mirrorChars(input,k):
- original = 'abcdefghijklmnopqrstuvwxyz'
- reverse = 'zyxwvutsrqponmlkjihgfedcba'
- dictChars = dict(zip(original,reverse))
- prefix = input[0:k-1]
- suffix = input[k-1:]
- mirror = ''
- for i in range(0,len(suffix)):
- mirror = mirror + dictChars[suffix[i]]
- print (prefix+mirror)
- if __name__ == "__main__":
- while True:
- print("\n============ Find Mirror Characters in a String Using a Dictionary ============\n")
- my_string = input("Enter a string: ")
- i = 3
- mirrorChars(my_string,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 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
Add new comment
- 7 views