Replace every nth letter in a string

后端 未结 7 903
渐次进展
渐次进展 2021-01-19 03:40

I\'m writing a function to replace every n-th letter from a string

def replaceN(str, n):
   for i in range(len(str)):
     n=str[i]
     newStr=str.replace(         


        
7条回答
  •  情书的邮戳
    2021-01-19 04:00

    You've put your return within the loop so after the first iteration it returns the string without the rest of the string being replaced. The string should be returned once the loop has completely finished. Something like this should work:

    def replaceN(str, n):
        for i in range(len(str)):
            n=str[i]
            newStr=str.replace(n, "*")
        return newStr
    

提交回复
热议问题