Python字符串转换为ASCII码并返回
我需要将输入的字符串转换为ASCII值,添加三个,然后打印出所有字母都已更改的新字符串 我已经到了可以在列表中获取ASCII值的地步,但是我很难将其转换回字符串 这是我到目前为止写的代码Python字符串转换为ASCII码并返回,python,string,python-2.7,encryption,ascii,Python,String,Python 2.7,Encryption,Ascii,我需要将输入的字符串转换为ASCII值,添加三个,然后打印出所有字母都已更改的新字符串 我已经到了可以在列表中获取ASCII值的地步,但是我很难将其转换回字符串 这是我到目前为止写的代码 def encrypt (x): #function for encrypting #y=[ord(c) for c in x] #turns strings into values this works #y=[3+ ord(c) for c in x] #adds 3 to the values of t
def encrypt (x): #function for encrypting
#y=[ord(c) for c in x] #turns strings into values this works
#y=[3+ ord(c) for c in x] #adds 3 to the values of the letters turned to number this also works
y=str([3+ ord(c) for c in x]) # this does not do what I expected it to do. Neither does char
print(y)
'''def decrypt (x):
y=str([-3 + ord(c) for c in x])
print(y)
'''
x=str(input("Enter something to be encrypted: ")) #gets string to encrypted
encrypt (x) #calls function sends the entered sentence
'''
x=str(input("Enter something to be decrypted: ")) #gets string to decrypted
decrypt (x)
'''
我注释掉了返回的第二部分,如果我能让它将更改了字母的字符串返回给我,那么我可能可以找出其余的部分
感谢您提供的任何帮助。
str
,应用于整数列表,正如您所发现的,它会给您一个类似于[65,66,67]
的字符串,而不是ABC
。要将单个整数转换回字符串,可以使用chr
。然后,要对整个整数列表执行此操作,请使用join
:
y = ''.join(chr(3 + ord(c)) for c in x)
str
,应用于整数列表,正如您所发现的,会给您一个类似[65,66,67]
的字符串,而不是ABC
。要将单个整数转换回字符串,可以使用chr
。然后,要对整个整数列表执行此操作,请使用join
:
y = ''.join(chr(3 + ord(c)) for c in x)
str
,应用于整数列表,正如您所发现的,会给您一个类似[65,66,67]
的字符串,而不是ABC
。要将单个整数转换回字符串,可以使用chr
。然后,要对整个整数列表执行此操作,请使用join
:
y = ''.join(chr(3 + ord(c)) for c in x)
str
,应用于整数列表,正如您所发现的,会给您一个类似[65,66,67]
的字符串,而不是ABC
。要将单个整数转换回字符串,可以使用chr
。然后,要对整个整数列表执行此操作,请使用join
:
y = ''.join(chr(3 + ord(c)) for c in x)