Reassigning letters in an alphet to a higher letter in python?

后端 未结 2 1578
被撕碎了的回忆
被撕碎了的回忆 2021-01-29 09:30

If I am building a basic encryption program in python that reassigns A to C and D to F and so on, what is a simple algorithm I could use to do this? I have a list named alphabet

2条回答
  •  忘掉有多难
    2021-01-29 10:08

    str.translate should be the easiest way:

    table = str.maketrans(
        "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
        "cdefghijklmnopqrstuvwxyzabCDEFGHIJKLMNOPQRSTUVWXYZAB"
    )
    s = "Test String"
    print(s.translate(table))
    

    Output:

    Vguv Uvtkpi
    

提交回复
热议问题