replace string in list code example

Example 1: how to replace string in list python

Use str. replace() to replace a string in a list
strings = ["a", "ab", "aa", "c"]
new_strings = []
for string in strings:
new_string = string. replace("a", "1") Modify old string.
new_strings. append(new_string) Add new string to list.
print(new_strings)

Example 2: replace by positions a string in a list with another string python

string = 'pythonhxamples'
position = 6
new_character = 'e'

string = string[:position] + new_character + string[position+1:]
print(string)

#output: pythonexamples