Answers for "python how to replace string"

25

python replace string

# string.replace(old, new, count),  no import is necessary
text = "Apples taste Good."
print(text.replace('Apples', 'Bananas'))          # use .replace() on a variable
Bananas taste Good.          <---- Output

print("Have a Bad Day!".replace("Bad","Good"))    # Use .replace() on a string
Have a Good Day!             <----- Output

print("Mom is happy!".replace("Mom","Dad").replace("happy","angry"))  #Use many times
Dad is angry!                <----- Output
Posted by: Guest on June-28-2020
3

python replace

string = "[Message]"
string = string.replace("[", "")#Removes all [
string = string.replace("]", "")#Removes all ]

print(string)
Posted by: Guest on September-28-2020
0

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
1

replace() python

#our text
text='Hello there how may I help you!'
     #replacing ("THIS" with "THIS") 
replaced_text=text.replace("!", "?")
print(replaced_text)
#output
"Hello there how may I help you?"
Posted by: Guest on September-30-2020
1

python replace

temp_str = 'this is a test'
print(temp_str.replace('is','IS')
print(temp_str)
      
#################### Result ###########################
      
thIS IS a test
this is a test
Posted by: Guest on July-02-2021

Python Answers by Framework

Browse Popular Code Answers by Language