-1

I have the following list of strings:

a = ['1234!zf', '5678!ras', 'abcd!ggt', 'defg!z', 'hijk!', 'lmnk!reom']

I want to replace the 4th character with another character (the character will always be the same in every string, I just don't know it, in this case it's '!'), for example '&'. How can I do that?

Resulting list of strings should look like this:

a = ['1234&zf', '5678&ras', 'abcd&ggt', 'defg&z', 'hijk&', 'lmnk&reom']

I know that strings in python are immutable and I honestly have no idea how to work with them. I tried all sorts of thins from foreach, using for with iterator, using .join(), using replace(), using list[4:], list[:4] and so many more and I just can't make this simple task work on python.

Note: Resulting list must be in list 'a'

Daniel Hao
  • 4,922
  • 3
  • 10
  • 23
Deutrys
  • 15
  • 5
  • Is it always a "!" (and there is only one "!") that you want to replace? – tdelaney Jul 09 '22 at 23:49
  • 1
    "I know that strings in python are immutable and I honestly have no idea how to work with them." Did you try [reading the documentation](https://docs.python.org/3/library/stdtypes.html#text-sequence-type-str), in order to understand *what those methods actually do*? You can also find out about them at the interactive prompt, using e.g. `help(str.replace)`. "using list[4:]" This is not relevant, because it is slicing **the list**, not the individual strings in the list. – Karl Knechtel Jul 10 '22 at 00:05
  • @DanielHao the question is too broad because it involves two separate procedures: doing something with each element of a list, and doing the replacement in a way that actually has an effect (i.e., keeping in mind that `.replace` will *create a new* string, doing something useful with that new string). I added a duplicate for each, so that the question can be answered quickly without attracting more attempts that don't help build the site; but questions like this should end up closed and deleted as "needs more focus" instead. – Karl Knechtel Jul 10 '22 at 00:09
  • Totally. Got the points - after seeing 2nd related links. Appreciate your *insight* on it. – Daniel Hao Jul 10 '22 at 00:10
  • @tdelaney no its not always a "!" and it is only one I want to replcace. – Deutrys Jul 10 '22 at 17:02
  • @KarlKnechtel I'll check the documentation out, thanks! – Deutrys Jul 10 '22 at 17:04

2 Answers2

0

You can use a for-loop to iterate through every index in array 'a', then assign a new string on each position such that new_string_i = prev_string[:4] + "&" + prev_string[5:]

Code snippet:

a = ['1234!zf', '5678!ras', 'abcd!ggt', 'defg!z', 'hijk!', 'lmnk!reom']
    for i in range(0, len(a)):
        str = a[i]
        a[i] = str[:4] + "&" + str[5:]
    print(a)

There are, of course, a lot of ways to approach this problem, but I find this one as being 'the most intuitive' and easy to comprehend.

Vlad Nitu
  • 147
  • 1
  • 11
0

There are multiple ways to do this, but string slicing is a good way to go. In this example, each string is sliced with the new character added at offset 4. Its built into a new list and then a slice encompassing the entire original list is replaced with the new values.

>>> a = ['1234!zf', '5678!ras', 'abcd!ggt', 'defg!z', 'hijk!', 'lmnk!reom']
>>> a[:] = [s[:4] + "&" + s[5:] for s in a]
>>> a
['1234&zf', '5678&ras', 'abcd&ggt', 'defg&z', 'hijk&', 'lmnk&reom']
tdelaney
  • 73,364
  • 6
  • 83
  • 116