Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python 3.8中将不同行上的字符打印成字符串_Python_Git - Fatal编程技术网

在Python 3.8中将不同行上的字符打印成字符串

在Python 3.8中将不同行上的字符打印成字符串,python,git,Python,Git,这是我的代码,目前它在不同的行上打印解密的凯撒密码字符。有没有办法将它们作为字符串添加到一行中?此外,还有一种可能的方法可以实现.isalpha()来解释未加密消息中的空格和问号等 """Cypher program.""" import string alphabet = string.ascii_lowercase message = "thequickbrownfoxjumpsoverthelazydog"

这是我的代码,目前它在不同的行上打印解密的凯撒密码字符。有没有办法将它们作为字符串添加到一行中?此外,还有一种可能的方法可以实现.isalpha()来解释未加密消息中的空格和问号等

"""Cypher program."""
import string

alphabet = string.ascii_lowercase
message = "thequickbrownfoxjumpsoverthelazydog"
key = 7
for char in message:
    new_char = key + (alphabet.index(char))
    if new_char > 25:
        new_char = new_char % 26
    print(alphabet[new_char])

我是Python新手,如果这是一个新手问题,非常抱歉


非常感谢热心帮助您的人。

您可以将字母表[新字符]添加到列表中,然后使用join将其打印为字符串。下面的示例代码(经过编辑,可以保留非字母数字的字符):


上帝保佑你,好先生!如果这个
message=“敏捷的棕色狐狸跳过了懒惰的狗。”
将取代
message=“快速的棕色狐狸跳过了懒惰的狗”
,那么有没有办法在字符串的末尾添加空格和句点。。我无意中编辑了你的帖子:(sorryFixed…(即使它得到了批准)而且你的解决方案也没有计算其他字符(?|!等等)谢谢@user2099394,刚刚更新了我的代码以包含特殊字符谢谢,我打算使用.isalpha()要定义一个句子中的字符是否不在字母表中,换句话说,有没有一种方法可以实现isalpha(),这样我就不必对一个常规句子中的每个不同标点符号使用多个if语句,例如
,,,!“#·%&/(
string.isalnum()->是一个内置str。但是您想检查是alpha还是删除其他字符?我想将字母表中没有的其他字符以不变的形式添加到尾字符串中,这样在解密后一个空格将保留为空格,一个点将保留为点,等等。我明白了。我将修改我的答案以包括:)
import string

alphabet = string.ascii_lowercase
message = "the quick brow???nxa2 fox jumps over the lazy dog"
key = 7
lst=[]
for char in message:
    if char.isalpha() is True:
        new_char = key + (alphabet.index(char))
        if new_char > 25:
            new_char = new_char % 26
        lst.append(alphabet[new_char])
    else:
        lst.append(char)
print(''.join(i for i in lst))
"""Cypher program."""
import string

alphabet = string.ascii_lowercase
message = "thequick0brownfox jumpsoverthelazydog"

def transform(char,key):
    if char.isalpha():
       new_char = key + (alphabet.index(char))
       if new_char > 25:
           new_char = new_char % 26
       return alphabet[new_char]
    return char

key = 7

# faster string comprehension
decripted = [transform(char,key) for char in message]
  
print(decripted)
# or 

# "".join - puts all elements of an array toghether in a string using a separator
print("".join(decripted))