# Atbash cipher in Python

PythonServer Side ProgrammingProgramming

#### Beyond Basic Programming - Intermediate Python

Most Popular

36 Lectures 3 hours

#### Practical Machine Learning using Python

Best Seller

91 Lectures 23.5 hours

#### Practical Data Science using Python

22 Lectures 6 hours

Suppose we have a lowercase alphabet string called text. We have to find a new string where every character in text is mapped to its reverse in the alphabet. As an example, a becomes z, b becomes y and so on.

So, if the input is like "abcdefg", then the output will be "zyxwvut"

To solve this, we will follow these steps −

• N := ASCII of ('z') + ASCII of ('a')

• return ans by joining each character from ASCII value (N - ASCII of s) for each character s in text

Let us see the following implementation to get better understanding −

## Example

Live Demo

class Solution:
def solve(self, text):
N = ord('z') + ord('a')
ans=''
return ans.join([chr(N - ord(s)) for s in text])
ob = Solution()
print(ob.solve("abcdefg"))
print(ob.solve("hello"))

## Input

"abcdefg"
"hello"

## Output

zyxwvut
svool
Updated on 02-Sep-2020 13:00:21