如何在python中使用不同列表中的字符替换列表中的字符

时间:2018-07-17 09:10:35

标签: 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)
for i in range(len(inputword)):

print (wordlist)

我正在尝试获取它,以便如果我输入“ Hello World”,它将返回“ Ifmmp xpsme”

不必返回确切的大小写(大写字母可以小写返回

1 个答案:

答案 0 :(得分:0)

如果我理解正确,则希望将用户提供的字符替换为字母的下一个字符。

假设mysqli_select_db($conn, $database_name); 应该变成z,请使用以下命令:

对于单字符输入(例如'a'),请使用以下命令:

a

对于整个字符串输入(例如“ hello world”),请使用以下代码:

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)

if wordlist[0] == 'z':
    #print('Sorry, I do not know what to do for this case')
    idx = 0
    new_word = alphabet[idx]
    print(new_word)

else:
    idx = alphabet.index(wordlist[0])
    new_word = alphabet[idx +1]
    print(new_word)

示例:

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)