0

In my current project, I encountered a scenario where I needed to insert an ampersand (&) before the first equals sign (=) that occurs in a string containing multiple equals signs. I came up with two methods for solving this problem:

First, we have the example string:

s = "x = y = z = 5"

Method 1: Convert the string into a list, insert the ampersand into the list via the index of the first '=' sign, then reassemble the string via the 'join' method:

def replace_by_index(s):
    idx = s.index('=')
    s = list(s)
    s.insert(idx, '&')
    s = "".join(s)

    return s

Method 2: Create a new string with the '&' placed between the two substrings created when dividing the original string at the index of the first '=':

def replace_at_index(s):
    idx = s.index('=')
    s = s[:idx-1] + '&' + s[idx:]

    return s

I imagine that method 2 is the preferred approach, but I am curious about the step-by-step efficiency of each method and whether anyone else considers method 1 more readable (method 1 reads more like natural language than method 2).

  • 2
    You do not want the `-1` in method 2. Remember that `s[:i]` and `s[i:]` together make up the entire string. You can measure the efficiency yourself using `timeit`, but method 2 certainly reads better. – Tim Roberts Nov 09 '22 at 01:06
  • 8
    I'd prefer `s.replace('=', '&=', 1)` – Yuri Ginsburg Nov 09 '22 at 01:12

1 Answers1

0

You have two good Pythonic choices (well, more than that but there are many ways to do string manipulation):

  1. https://docs.python.org/3/library/stdtypes.html#str.replace Which is preferred in simple cases like yours: Use Python's string.replace vs re.sub.
  2. https://docs.python.org/3/library/re.html?highlight=re#re.sub Great if you need to use regular expressions.

A lot will also depend on how consistent your input data is that you need to perform the replace on. Will you be filtering it first or searching for a field/case. That could mean you are using re already for finding the substitution case. If it is always the first case of '=' regardless (e.g. even if there is only one ever present), option 1 would be fine, but in many other cases it might not be (if only one '=' is present, or a line has a '>=' or '==' present, or the line is a comment containing '=', etc.) in which case you may need to use re to find your specific case first and the option #2 to replace.

MrMattBusby
  • 116
  • 5