Wednesday, July 16, 2014

How to replace text within a string

Code
oldstring = 'This is the string with text that needs to be replaced'   #1
newstring = oldstring.replace('needs to be ','had text ')              #2
print newstring                                                        #3


How this works - Line by Line

oldstring = 'This is the string with text that needs to be replaced'   #1
 
Defining oldstring to be string of text which i will use as an example


newstring = oldstring.replace('needs to be ','had text ')              #2

This line replaces text within the old string. 

print newstring                                                        #3

Printing the newstring to show that the desired text has been replaced.

No comments:

Post a Comment