在python中,如何用不同列表中的字符替换列表中的字符
所以我试图用字符串替换字母表中的下一个字母。我已经转换了输入,但我不知道从这里做什么? 这是我的密码:在python中,如何用不同列表中的字符替换列表中的字符,python,Python,所以我试图用字符串替换字母表中的下一个字母。我已经转换了输入,但我不知道从这里做什么? 这是我的密码: alphabet =['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'] wordlist = [] inputword = input("Please enter a string") wordlist = list(inputword
alphabet =['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
wordlist = []
inputword = input("Please enter a string")
wordlist = list(inputword)
for i in range(len(inputword)):
print (wordlist)
我正在尝试获取它,以便如果我输入“Hello World”,它将返回“Ifmmp xpsme”
它不必返回精确的大小写(大写字母可以作为小写字母返回)如果我理解正确,您希望将用户给定的字符替换为字母表的下一个字符 假设
z
应该变成a
使用以下选项:
对于单字符输入(例如“a”),请使用:
对于整个字符串输入(例如“hello world”),请使用以下命令:
示例:
USER_INPUT= 'hello world'
print(next_letter(USER_INPUT))
'ifmmp xpsme'
看看我的答案,让我知道应该用什么来代替
z
?也许,输入和期望输出的例子会让事情变得清晰,好吧,除了这个问题根本没有显示出任何努力之外,输入中的'z'
的功能性副本?看看我的答案。我对这个OK进行了注释,这样对1有效位置,我应该为整个字符串做什么?因为我不想要一个字符,它应该适用于一个句子哦。你想用下一个字符替换字符串中的每个字符吗?所以输入是一个完整的字符串而不是一个字符?对于Z,如果单词列表[0]='Z':new_word='a'
def next_letter(st):
index = 0
new_word = ""
alphapet = "abcdefghijklmnopqrstuvwxyzacd"
for i in st.lower():
if i.islower(): #check if i s letter
index = alphapet.index(i) + 1 #get the index of the following letter
new_word += alphapet[index]
else: #if not letter
new_word += i
return new_word
USER_INPUT = input('enter string:')
next_letter(USER_INPUT)
USER_INPUT= 'hello world'
print(next_letter(USER_INPUT))
'ifmmp xpsme'