Replacing instances of a character in a string

前端 未结 13 2695
谎友^
谎友^ 2020-11-22 07:19

This simple code that simply tries to replace semicolons (at i-specified postions) by colons does not work:

for i in range(0,len(line)):
     if (line[i]==\"         


        
13条回答
  •  鱼传尺愫
    2020-11-22 07:34

    To replace a character at a specific index, the function is as follows:

    def replace_char(s , n , c):
        n-=1
        s = s[0:n] + s[n:n+1].replace(s[n] , c) + s[n+1:]
        return s
    

    where s is a string, n is index and c is a character.

提交回复
热议问题