Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Program to find mirror characters in a string
This article teaches you how to write a Python program to find mirror characters in a string. A mirror character is the alphabetically opposite character − 'a' mirrors to 'z', 'b' to 'y', and so on.
Understanding Mirror Characters
Mirror characters are pairs of letters that are equidistant from both ends of the alphabet. For example:
'a' (1st letter) mirrors to 'z' (26th letter)
'b' (2nd letter) mirrors to 'y' (25th letter)
'c' (3rd letter) mirrors to 'x' (24th letter)
Input-Output Example
Given a string and a position, we mirror characters from that position onwards ?
Input: string = "Coding", position = 3 Output: "Cowrmt"
Here, characters from position 3 onwards ('d', 'i', 'n', 'g') are mirrored to ('w', 'r', 'm', 't').
Using Loops and ASCII Values
This approach uses ASCII values to calculate mirror characters directly ?
def mirror_characters(text, position):
result = ""
# Keep characters before position unchanged
for i in range(position):
result += text[i]
# Mirror characters from position onwards
for i in range(position, len(text)):
char = text[i]
if 'a'
Original: Coding
Mirrored from position 2: Cowrmt
Using Dictionary Mapping
This approach creates a dictionary to map each letter to its mirror character ?
def mirror_with_dict(text, position):
# Create mirror dictionary
alphabet = 'abcdefghijklmnopqrstuvwxyz'
reversed_alphabet = 'zyxwvutsrqponmlkjihgfedcba'
mirror_dict = dict(zip(alphabet, reversed_alphabet))
# Add uppercase mapping
upper_dict = dict(zip(alphabet.upper(), reversed_alphabet.upper()))
mirror_dict.update(upper_dict)
result = ""
# Keep prefix unchanged
result += text[:position]
# Mirror suffix
for char in text[position:]:
if char in mirror_dict:
result += mirror_dict[char]
else:
result += char # Non-alphabetic characters unchanged
return result
# Example usage
text = "TutorialsPoint"
position = 3
result = mirror_with_dict(text, position)
print(f"Original: {text}")
print(f"Mirrored from position {position}: {result}")
Original: TutorialsPoint
Mirrored from position 3: TutlirzohKlrmg
Complete Example with User Input
Here's a complete program that handles both uppercase and lowercase letters ?
def mirror_string(text, start_pos):
"""
Mirror characters in a string from given position onwards
"""
def get_mirror(char):
if 'a' '{result}'")
'Hello' from position 2 -> 'Heooo'
'Python' from position 1 -> 'Pbgsln'
'abc123xyz' from position 3 -> 'abc123cba'
Comparison
| Method | Pros | Cons |
|---|---|---|
| ASCII Values | Memory efficient, direct calculation | More complex logic |
| Dictionary | Simple lookup, readable code | Uses extra memory for dictionary |
Conclusion
Mirror characters can be found using ASCII arithmetic or dictionary mapping. The ASCII method is more memory-efficient, while the dictionary approach offers cleaner, more readable code.
