Replacing instances of a character in a string

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

    to use the .replace() method effectively on string without creating a separate list for example take a look at the list username containing string with some white space, we want to replace the white space with an underscore in each of the username string.

    usernames = ["Joey Tribbiani", "Monica Geller", "Chandler Bing", "Phoebe Buffay"]
    

    to replace the white spaces in each username consider using the range function in python.

    for i in range(len(usernames)):
        usernames[i] = usernames[i].lower().replace(" ", "_")
    
    print(usernames)
    

提交回复
热议问题