Python - Mirror Image of String Last Updated : 24 Apr, 2023 Comments Improve Suggest changes Like Article Like Report Given a String, perform its mirror imaging, return "Not Possible" if mirror image not possible using english characters. Input : test_str = 'boid' Output : doib Explanation : d replaced by b and vice-versa as being mirror images. Input : test_str = 'gfg' Output : Not Possible Explanation : Valid Mirror image not possible. Method : Using loop + lookup dictionary This is one way in which this task can be performed. In this, we construct lookup dictionary for all valid mirrorable english characters, then perform task of access from them. Python3 # Python3 code to demonstrate working of # Mirror Image of String # Using Mirror Image of String # initializing strings test_str = 'void' # printing original string print("The original string is : " + str(test_str)) # initializing mirror dictionary mir_dict = {'b':'d', 'd':'b', 'i':'i', 'o':'o', 'v':'v', 'w':'w', 'x':'x'} res = '' # accessing letters from dictionary for ele in test_str: if ele in mir_dict: res += mir_dict[ele] # if any character not present, flagging to be invalid else: res = "Not Possible" break # printing result print("The mirror string : " + str(res)) OutputThe original string is : void The mirror string : voib Time Complexity: O(n) Space Complexity: O(n) Method 2 : using string slicing to reverse the string and a lookup dictionary to replace the mirrored characters. step by step approach: Define the input string.Define a dictionary that maps mirrored characters to their respective values.Reverse the input string using string slicing.Iterate over the reversed string and replace each character with its mirror image from the dictionary. If the character is not present in the dictionary, set the result string to "Not Possible" and break the loop.Reverse the result string back to its original order.Print the original and mirror strings. Python3 # Python3 code to demonstrate working of # Mirror Image of String # Using String Slicing and Lookup Dictionary # Define input string test_str = 'void' # Define mirror dictionary mir_dict = {'b': 'd', 'd': 'b', 'i': 'i', 'o': 'o', 'v': 'v', 'w': 'w', 'x': 'x'} # Reverse the input string rev_str = test_str[::-1] # Initialize result string res = '' # Iterate over reversed string and replace mirrored characters for ele in rev_str: if ele in mir_dict: res += mir_dict[ele] else: res = "Not Possible" break # Reverse the result string mir_str = res[::-1] # Print the original and mirror strings print("The original string is : " + str(test_str)) print("The mirror string : " + str(mir_str)) OutputThe original string is : void The mirror string : voib The time complexity of this approach is O(n) as it involves iterating over the string only once. The auxiliary space complexity is also O(n) because we create a new reversed string and a new mirrored string. Comment More infoAdvertise with us Next Article Python - Mirror Image of String manjeet_04 Follow Improve Article Tags : Python Python Programs Python string-programs Practice Tags : python Similar Reads List of strings in Python A list of strings in Python stores multiple strings together. In this article, weâll explore how to create, modify and work with lists of strings using simple examples.Creating a List of StringsWe can use square brackets [] and separate each string with a comma to create a list of strings.Pythona = 2 min read Python | Lowercase first character of String The problem of capitalizing a string is quite common and has been discussed many times. But sometimes, we might have a problem like this in which we need to convert the first character of the string to lowercase. Let us discuss certain ways in which this can be performed. Method #1: Using string sli 4 min read Python | Add one string to another The concatenation of two strings has been discussed multiple times in various languages. But the task is how to add to a string in Python or append one string to another in Python. Example Input: 'GFG' + 'is best' Output: 'GFG is best' Explanation: Here we can add two string using "+" operator in Py 5 min read String Interning in Python String interning is a memory optimization technique used in Python to enhance the efficiency of string handling. In Python, strings are immutable, meaning their values cannot be changed after creation. String interning, or interning strings, involves reusing existing string objects rather than creat 2 min read How to copy a string in Python Creating a copy of a string is useful when we need a duplicate of a string to work with while keeping the original string intact, strings in Python are immutable which means they can't be altered after creation, so creating a copy sometimes becomes a necessity for specific use cases.Using SlicingSli 2 min read Python | Find last occurrence of substring Sometimes, while working with strings, we need to find if a substring exists in the string. This problem is quite common and its solution has been discussed many times before. The variation of getting the last occurrence of the string is discussed here. Let's discuss certain ways in which we can fin 8 min read Similarity Metrics of Strings - Python In Python, we often need to measure the similarity between two strings. For example, consider the strings "geeks" and "geeky" âwe might want to know how closely they match, whether for tasks like comparing user inputs or finding duplicate entries. Let's explore different methods to compute string si 3 min read How to Create String Array in Python ? To create a string array in Python, different methods can be used based on the requirement. A list can store multiple strings easily, NumPy arrays offer more features for large-scale data and the array module provides type-restricted storage. Each method helps in managing collections of text values 2 min read Put String in A Set in Python Imagine you have a magical box in Python that only stores unique items â that's exactly what a set is! Sets are like special containers designed to hold a bunch of different things without any duplicates. In Python, they provide a versatile and efficient way to manage collections of distinct element 3 min read Convert String to bytes-Python The goal here is to convert a string into bytes in Python. This is essential for working with binary data or when encoding strings for storage or transmission. For example, given the string "Hello", these methods can convert it into a byte representation like b'Hello'. Letâs explore different method 2 min read Like