python code to replace WORDS.docx - Python In[4#Here's our text a ='bcz following are bcz graphical(non-control characters defined bcz#Using replace

python code to replace WORDS.docx - Python In[4#Here's our...

This preview shows page 1 - 2 out of 2 pages.

Python: In [4]: #Here's our text a = ( 'bcz following are bcz graphical (non-control) characters defined bcz' ) #Using replace method #The syntax of replace() is:str.replace(old, new [, count]) a = a . replace( 'bcz' , 'because' ) a Out[4]: 'because following are because graphical (non-control) characters defined because' In [5]: # replacing text in a file with open ( 'test.txt' , 'w' ) as f: f . write( 'bcz following are bcz graphical (non-control) characters defined bcz' ) with open ( 'test.txt' , 'r' ,) as f: a = f . read() #f.read reads file #The syntax of replace() is:str.replace(old, new [, count]) a = a . replace( 'bcz' , 'because' ) with open ( 'test.txt' , 'w' ,) as f: f . write(a) #f.write writes the file into disk f . close() #f.close closes the connection with the file f = open ( 'test.txt' , 'r' ) f_contents = f . read() print (f_contents) f . close() because following are because graphical (non-control) characters defined
Image of page 1
Image of page 2

You've reached the end of your free preview.

Want to read both pages?

  • Fall '19
  • Vishakha

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture