How to use the ROT13 (shifts every letter by 13 places) transform on a string in Python

1 Answer

0 votes
import codecs


def rot13(text: str) -> str:
    """
    Apply the ROT13 cipher to a string.

    ROT13 rotates each letter by 13 positions in the alphabet.
    Non-alphabetic characters are left unchanged.
    Applying ROT13 twice returns the original text.

    Args:
        text: The text to encode or decode.

    Returns:
        The ROT13-transformed string.
    """
    return codecs.encode(text, "rot_13")


def main() -> None:
    input_text = "Hello, World! 123"

    encoded = rot13(input_text)
    decoded = rot13(encoded)

    print(f"Input   : {input_text}")
    print(f"Encoded : {encoded}")
    print(f"Decoded : {decoded}")


if __name__ == "__main__":
    main()


'''
run:

Input   : Hello, World! 123
Encoded : Uryyb, Jbeyq! 123
Decoded : Hello, World! 123

'''

 



answered 3 days ago by avibootz

Related questions

...