How do you replace all occurrences in a string in python?

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Sometimes, while working with Python strings, we can have a problem in which we need to replace all occurrences of a substring with other.

    Input : test_str = “geeksforgeeks” s1 = “geeks” s2 = “abcd” 

    Output : test_str = “abcdforabcd” Explanation : We replace all occurrences of s1 with s2 in test_str. 

    Input : test_str = “geeksforgeeks” s1 = “for” s2 = “abcd” 

    Output : test_str = “geeksabcdgeeks”

     Approach 1

    We can use inbuilt function replace present in python3 to replace all occurrences of substring.

    Implementation using the inbuilt function:-

    Python3

    input_string = "geeksforgeeks"

    s1 = "geeks"

    s2 = "abcd"

    input_string = input_string.replace[s1, s2]

    print[input_string]

    Approach 2:

    Splitting the string by substring and then replacing with the new string.split[] function is used.

    Python3

    test_str="geeksforgeeks"

    s1="geeks"

    s2="abcd"

    s=test_str.split[s1]

    new_str=""

    for i in s:

        if[i==""]:

            new_str+=s2

        else:

            new_str+=i

    print[new_str]

    The Time and Space Complexity for all the methods are the same:

    Time Complexity: O[n]

    Auxiliary Space: O[n]


    View Discussion

    Improve Article

    Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    The replace[] in Python returns a copy of the string where all occurrences of a substring are replaced with another substring. 

    Syntax of replace[]

    Syntax: string.replace[old, new, count]

    Parameters: 

    • old – old substring you want to replace.
    • new – new substring which would replace the old substring.
    • count – [Optional ] the number of times you want to replace the old substring with the new substring. 

    Return Value : It returns a copy of the string where all occurrences of a substring are replaced with another substring. 

    Replace All Instances of a Single Character using replace[]

    In this example, we are only replacing a single character from a given string. The Python replace[] method is case-sensitive, and therefore it performs a case-sensitive substring substitution, i.e. R in FOR is unchanged.

    Python3

    string = "grrks FOR grrks"

    new_string = string.replace["r", "e" ]

    print[string]

    print[new_string]

    Output : 

    grrks FOR grrks
    geeks FOR geeks

    Replace All Instances of a String

    Here, we replaced all the geeks with GeeksforGeeks using replace[] function.

    Python3

    string = "geeks for geeks \ngeeks for geeks"

    print[string]

    print[string.replace["geeks", "GeeksforGeeks"]]

    Output : 

    geeks for geeks 
    geeks for geeks
    GeeksforGeeks for GeeksforGeeks 
    GeeksforGeeks for GeeksforGeeks

    Replace Only a Certain Number of Instances using replace[]

    In this example, we are replacing certain numbers of words. i.e. “ek” with “a” with count=3.

    Python3

    string = "geeks for geeks geeks geeks geeks"

    print[string.replace["e", "a"]]

    print[string.replace["ek", "a", 3]]

    Output:  

    gaaks for gaaks gaaks gaaks gaaks
    geas for geas geas geeks geeks

    How do you replace all occurrences in a string?

    To replace all occurrences of a substring in a string by a new one, you can use the replace[] or replaceAll[] method: replace[] : turn the substring into a regular expression and use the g flag.

    How do you replace multiple items in a string in Python?

    Replace Multiple Characters in a String in Python.
    Use str.replace[] to Replace Multiple Characters in Python..
    Use re.sub[] or re.subn[] to Replace Multiple Characters in Python..
    translate[] and maketrans[] to Replace Multiple Characters in Python..

    Is there a replace all function in Python?

    The replace[] method is a built-in functionality offered in Python programming. It replaces all the occurrences of the old substring with the new substring. Replace[] returns a new string in which old substring is replaced with the new substring.

    How do you change every instance of a word in Python?

    When using the . replace[] Python method, you are able to replace every instance of one specific character with a new one. You can even replace a whole string of text with a new line of text that you specify.

    Chủ Đề