Answers for "replace substring in string python"

1

replace substrings in python

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
Posted by: Guest on January-03-2022
2

python how to replace a certain string in text

# 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)
Posted by: Guest on August-14-2021

Code answers related to "replace substring in string python"

Python Answers by Framework

Browse Popular Code Answers by Language