gpt4 book ai didi

python - 如何识别字符串中的非字母

转载 作者:太空宇宙 更新时间:2023-11-04 10:03:00 24 4
gpt4 key购买 nike

大家好 :) 我正在构建一个脚本来使用简单的文本旋转 (ROT) 对文本进行编码。该脚本运行良好,但我遇到了问题,它还会旋转所有符号,例如 [spaces,!,?,.] 我正在使用 ascii 表来执行此操作,我该怎么做才能避免旋转此类字符?

def rot13(input,key): #Function to code a text with caeser chyper.
if key > 25:
key = 25
elif key < 2:
key = 2
finaltext = ''
for letter in input:
num = ord(letter)
if (num + key) > 122: #If the final number is greater than 122..
x = (num + key) - 122
finaltext += chr(x + ord('a') - 1)
elif((num + key <= 122)):
finaltext += chr(num + key)
print(finaltext)

最佳答案

在“旋转”你的角色之前,添加一个检查以查看它是否是字母数字:

if letter.isalpha():
# Do your thing
else:
finaltext += letter

关于python - 如何识别字符串中的非字母,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42516028/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com