Replace A Character Or Substring In A Python String
January - 2022
string = 'the quick brown fox'
new_string = string.replace('quick', 'slow')
print(new_string)
Outputs:
the slow brown fox
If you only want to replace a certain number, add a third parameter with the count.
string = 'a1 a2 a3 a4 a5 a6'
new_string = string.replace('a', 'B', 3)
print(new_string)
Outputs:
B1 B2 B3 a4 a5 a6