Find the Mirror Image of a String using Python

In the given problem statement we are required to find the mirror image of a given string with the help of Python code.

Understanding The Problem

The problem at hand is to find the mirror image of the given string. The mirror image is the changed version of the given string in which every character is replaced with the mirror image of that character. Or we can say it is the reflection of the string. In real life we see ourselves in the mirror so our right part is visible in the left side in the mirror and similarly the left part shows in the right side.

However, the concept here is simpler we reverse the string and optionally apply character transformations to simulate visual mirroring.

Input-Output Scenario

Following is an input and its output example of mirror image of the given string

Input string = TUTOR
Output string: ROTUT

Logic for The Above Problem

To solve this problem, first we will reverse the given string with the help of the reversed function of python. And then we will replace each character with the corresponding mirror image of that character.

Algorithm

  • Step 1 Define the function called get_mirror_image. And pass an argument called input_string.

  • Step 2 Define the mirror characters dictionary as mirror_chars, this will contain all the mirror characters as a dictionary.

  • Step 3 Iterate over the reversed string and replace the character with its mirror image from the above dictionary.

  • Step 4 Reverse the input string using the reversed function. And store the mirror string in mirror_string variable.

  • Step 5 Now get the input from the user and print the original and mirror strings.

Example

def get_mirror_image(input_string):
    mirror_chars = {
        'A': 'A', 'B': 'B', 'C': 'C', 'D': 'D', 'E': 'E', 'F': 'F', 'G': 'G', 'H': 'H', 'I': 'I', 
        'J': 'J', 'K': 'K', 'L': 'L', 'M': 'M', 'N': 'N', 'O': 'O', 'P': 'P', 'Q': 'Q', 'R': 'R', 
        'S': 'S', 'T': 'T', 'U': 'U', 'V': 'V', 'W': 'W', 'X': 'X', 'Y': 'Y', 'Z': 'Z', ' ': ' ', 
        'a': 'a', 'b': 'b', 'c': 'c', 'd': 'd', 'e': 'e', 'f': 'f', 'g': 'g', 'h': 'h', 'i': 'i', 
        'j': 'j', 'k': 'k', 'l': 'l', 'm': 'm', 'n': 'n', 'o': 'o', 'p': 'p', 'q': 'q', 'r': 'r', 
        's': 's', 't': 't', 'u': 'u', 'v': 'v', 'w': 'w', 'x': 'x', 'y': 'y', 'z': 'z'
    }

    # Reverse and replace characters with their mirror characters
    mirror_string = ''.join(mirror_chars.get(char, char) for char in reversed(input_string))

    return mirror_string

# Testing the function
original_string = "Tutorialspoint"
mirror_string = get_mirror_image(original_string)

print("Original String:", original_string)
print("Mirror image:", mirror_string)
Original String: Tutorialspoint
Mirror image: tniopslairotuT

Simple String Reversal Approach

For basic mirror imaging, we can simply reverse the string without character mapping ?

def simple_mirror(text):
    return text[::-1]

# Example usage
original = "PYTHON"
mirrored = simple_mirror(original)

print(f"Original: {original}")
print(f"Mirrored: {mirrored}")
Original: PYTHON
Mirrored: NOHTYP

Using Built-in Functions

Python's reversed() function can also create the mirror image ?

def mirror_with_reversed(text):
    return ''.join(reversed(text))

# Example
word = "HELLO"
result = mirror_with_reversed(word)

print(f"Original: {word}")
print(f"Mirror: {result}")
Original: HELLO
Mirror: OLLEH

Comparison

Method Complexity Best For
String slicing [::-1] O(n) Simple and fast
reversed() function O(n) Memory efficient
Character mapping O(n) Visual mirroring effects

Complexity

In the code we have used a dictionary to store all the mirror characters. The time complexity is O(n) where n is the length of the input string. The reversed function iterates through the string once, and dictionary lookup takes O(1) constant time. So overall complexity is O(n).

Conclusion

String mirroring in Python can be achieved through simple reversal using slicing or the reversed() function. For basic mirroring, string slicing [::-1] is the most efficient approach.

Updated on: 2026-03-27T15:22:02+05:30

692 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements