You could use str.replace()
or .split()
and enumerate()
to accomplish this
Using str.replace()
s = "Delhi is the capital of India."
s = s.replace('the', '123the123')
# Delhi is 123the123 capital of India.
Using .split()
and enumerate()
s = "Delhi is the capital of India."
s = s.split()
for i, v in enumerate(s):
if v == 'the':
s[i] = '123the123'
s = ' '.join(s)
' '.join()
with a generator expression
print(' '.join("123the123" if w=="the" else w for w in s.split()))
Further reading
https://docs.python.org/3/library/stdtypes.html#string-methods
https://en.m.wikipedia.org/wiki/Scunthorpe_problem