xxxxxxxxxx
'''Write a python program using functions to display every occurrence
of word “yours” in file.txt as “mine”.'''
# input
def yours_mine():
f=open('file.txt','r+')
p=f.read()
x=p.replace('yours','mine')
f.seek(0)
f.write(x)
f.close()
yours_mine()
'''file.txt'''
The life that I have
Is all that I have
And the life that I have
Is yours
The love that I have
Of the life that I have
Is yours and yours and yours.
A sleep I shall have
A rest I shall have
Yet death will be but a pause
For the peace of my years
In the long green grass
Will be yours and yours and yours
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
#output:
'''file.txt'''
''The life that I have
Is all that I have
And the life that I have
Is mine
The love that I have
Of the life that I have
Is mine and mine and mine.
A sleep I shall have
A rest I shall have
Yet death will be but a pause
For the peace of my years
In the long green grass
Will be mine and mine and mine.''
xxxxxxxxxx
const p = 'The quick brown fox jumps over the lazy dog. If the dog reacted, was it really lazy?';
console.log(p.replace('dog', 'monkey'));
// expected output: "The quick brown fox jumps over the lazy monkey. If the dog reacted, was it really lazy?"
const regex = /Dog/i;
console.log(p.replace(regex, 'ferret'));
// expected output: "The quick brown fox jumps over the lazy ferret. If the dog reacted, was it really lazy?"
xxxxxxxxxx
const myText = 'The weather is cold';
const newString = myText.replace('cold', 'warm');
console.log(newString); // Should print "The weather is warm"
// the replace() string function takes a string,
// replaces one substring with another, and returns
// a new string with the replacement made
xxxxxxxxxx
#replace(a,b) is the function which can replace the 'abc' word to 'klmn' word
example
a=input(' sentence :')
b=input(' word to be replaced :')
c=input(' word to be replaced with :')
d=a.replace(b,c)
print(d)
#output:
'''
sentence :richard play with pixis and natasha, rahim play along johan,rosel and ethan
word to be replaced :play
word to be replaced with :go
richard go with pixis and natasha, rahim go along johan,rosel and ethan
'''