如何将变量打印为输入长度的字符串?

时间:2018-02-12 18:04:50

标签: python

我的任务是当我输入' z'我得到了' w'

的结果

这很有效。

但是,如果我输入' zzzz'只有一个' w'输出。

我的问题是如何打印' w'我输入z的次数。

我对StackOverflow相对较新。如果我违反任何规则或者我的问题没有正确措辞,我很抱歉。

z='w'
while True:
    plaintext=input('enter a word to get its ciphertext')            
    i=list(plaintext)
    print (i)
    if 'z' in plaintext
        print("w")

2 个答案:

答案 0 :(得分:1)

你太复杂了。从input()得到的变量是一个字符串。只需使用str.replace()

while True:
    plaintext = input('enter a word to get its ciphertext')
    plaintext_rep = plaintext.replace('z','w')
    print(plaintext_rep)

答案 1 :(得分:0)

尝试循环浏览文本以获取所有字母

z='w'
while True:
    plaintext=input('enter a word to get its ciphertext')
    i=plaintext

    for letter in i :
        if(letter == 'z'):
            print ('w')