Replacing instances of a character in a string

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

    You cannot simply assign value to a character in the string. Use this method to replace value of a particular character:

    name = "India"
    result=name .replace("d",'*')
    

    Output: In*ia

    Also, if you want to replace say * for all the occurrences of the first character except the first character, eg. string = babble output = ba**le

    Code:

    name = "babble"
    front= name [0:1]
    fromSecondCharacter = name [1:]
    back=fromSecondCharacter.replace(front,'*')
    return front+back
    

提交回复
热议问题