Replacing instances of a character in a string

前端 未结 13 2379
谎友^
谎友^ 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
    
    0 讨论(0)
  • 2020-11-22 07:48

    This should cover a slightly more general case, but you should be able to customize it for your purpose

    def selectiveReplace(myStr):
        answer = []
        for index,char in enumerate(myStr):
            if char == ';':
                if index%2 == 1: # replace ';' in even indices with ":"
                    answer.append(":")
                else:
                    answer.append("!") # replace ';' in odd indices with "!"
            else:
                answer.append(char)
        return ''.join(answer)
    

    Hope this helps

    0 讨论(0)
  • 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)
    
    0 讨论(0)
  • 2020-11-22 07:56
    names = ["Joey Tribbiani", "Monica Geller", "Chandler Bing", "Phoebe Buffay"]
    
    usernames = []
    
    for i in names:
        if " " in i:
            i = i.replace(" ", "_")
        print(i)
    

    Output: Joey_Tribbiani Monica_Geller Chandler_Bing Phoebe_Buffay

    0 讨论(0)
  • 2020-11-22 07:57

    I wrote this method to replace characters or replace strings at a specific instance. instances start at 0 (this can easily be changed to 1 if you change the optional inst argument to 1, and test_instance variable to 1.

    def replace_instance(some_word, str_to_replace, new_str='', inst=0):
        return_word = ''
        char_index, test_instance = 0, 0
        while char_index < len(some_word):
            test_str = some_word[char_index: char_index + len(str_to_replace)]
            if test_str == str_to_replace:
                if test_instance == inst:
                    return_word = some_word[:char_index] + new_str + some_word[char_index + len(str_to_replace):]
                    break
                else:
                    test_instance += 1
            char_index += 1
        return return_word
    
    0 讨论(0)
  • 2020-11-22 07:59

    Turn the string into a list; then you can change the characters individually. Then you can put it back together with .join:

    s = 'a;b;c;d'
    slist = list(s)
    for i, c in enumerate(slist):
        if slist[i] == ';' and 0 <= i <= 3: # only replaces semicolons in the first part of the text
            slist[i] = ':'
    s = ''.join(slist)
    print s # prints a:b:c;d
    
    0 讨论(0)
提交回复
热议问题