How to replace a character with another character in a list of string
Question:
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’
Answers:
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.
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']
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’
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.
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']