Wednesday, November 30, 2011

Python String replace

I came across a point where the incoming user string had to be replaced with something i desire . Considering the simplicity of the activity i expected python to have some user defined function . might be a simple thing , but really useful...

replace( old , new, [max count]).
if you have the below
Code :
sample = "this is a test string"
print sample.replace("test" , "long test")
the result will look like this
"this is a long test string"
you dont have to import any library in specific.
the "max" from the syntax constitutes to the number of times the string has to be replaced.

No comments:

Post a Comment