...

/

Solution: Replace All Occurrences of a Character in a String

Solution: Replace All Occurrences of a Character in a String

Learn how to find and replace all occurrences of a specific character in a string.

We'll cover the following...

The solution to the problem of finding and replacing all occurrences of a specific character in a string is given below.

Solution

Press + to interact
s = "The Terrible Tiger Tore The Towel"
a = 'T'
b = 't'
pos = s.find(a, 0)
print(pos)
pos = s.find(a, pos + 1)
print(pos)
pos = s.find(a, pos + 1)
print(pos)
pos = s.find(a, pos + 1)
print(pos)
pos = s.find(a, pos + 1)
print(pos)
pos = s.find(a, pos + 1)
print(pos)
pos = s.find(a, pos + 1)
print(pos)
c = s.count(a)
s = s.replace(a, b, c)
print(s)
...