https en wikipedia orgwikiatbash import string def atbashslowsequence str str output for i in sequence extract ordi if 65 extract 90 output chr155 extract elif 97 extract 122 output chr219 extract else output i return output def atbashsequence str str letters string asciiletters lettersreversed string asciilowercase 1 string asciiuppercase 1 return join lettersreversedletters indexc if c in letters else c for c in sequence def benchmark none atbash_slow abcdefg zyxwvut atbash_slow aw 123bx zd 123yc atbash abcdefg zyxwvut atbash aw 123bx zd 123yc let s benchmark our functions side by side
import string


def atbash_slow(sequence: str) -> str:
    
    output = ""
    for i in sequence:
        extract = ord(i)
        if 65 <= extract <= 90:
            output += chr(155 - extract)
        elif 97 <= extract <= 122:
            output += chr(219 - extract)
        else:
            output += i
    return output


def atbash(sequence: str) -> str:
    
    letters = string.ascii_letters
    letters_reversed = string.ascii_lowercase[::-1] + string.ascii_uppercase[::-1]
    return "".join(
        letters_reversed[letters.index(c)] if c in letters else c for c in sequence
    )


def benchmark() -> None:
    
    from timeit import timeit

    print("Running performance benchmarks...")
    setup = "from string import printable ; from __main__ import atbash, atbash_slow"
    print(f"> atbash_slow(): {timeit('atbash_slow(printable)', setup=setup)} seconds")
    print(f">      atbash(): {timeit('atbash(printable)', setup=setup)} seconds")


if __name__ == "__main__":
    for example in ("ABCDEFGH", "123GGjj", "testStringtest", "with space"):
        print(f"{example} encrypted in atbash: {atbash(example)}")
    benchmark()