Replacing instances of a character in a string

前端 未结 13 2645
谎友^
谎友^ 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:47

    You can do the below, to replace any char with a respective char at a given index, if you wish not to use .replace()

    word = 'python'
    index = 4
    char = 'i'
    
    word = word[:index] + char + word[index + 1:]
    print word
    
    o/p: pythin
    

提交回复
热议问题