xxxxxxxxxx
#user replace
string = "Hello, World!"
string = string.replace("World", "Universe")
print(string) #prints Hello, Universe!
xxxxxxxxxx
s = 'one two one two one'
print(s.replace(' ', '-'))
# Output -
# one-two-one-two-one
s = 'one two one two one'
print(s.replace(' ', ''))
# Output -
# onetwoonetwoone
xxxxxxxxxx
# Read in the file
with open('file.txt', 'r') as file :
filedata = file.read()
# Replace the target string
filedata = filedata.replace('ram', 'abcd')
# Write the file out again
with open('file.txt', 'w') as file:
file.write(filedata)
xxxxxxxxxx
s = 'Some String test'
print(s.replace(' ', '-'))
# Output
# Some-String-test
xxxxxxxxxx
s1 = 'The theory of data science is of the utmost importance.'
s2 = 'practice'
print('The new sentence: {}'.format(s1.replace('theory', s2)))
# Ouput
# The new sentence: The practice of data science is of the utmost importance.
xxxxxxxxxx
text='ramkumar'
text=text.replace("mku","") #'' is empty
print(text)
ans:
ramar