Tutorials Point


  Python Basics
  Python Advanced
  Python Useful References
  Python Useful Resources
  Selected Reading

© 2011 TutorialsPoint.COM


  Home     References     Discussion Forums     About TP  

Python String replace() Method


previous next AddThis Social Bookmark Button


Description:

This method returns a copy of the string in which the occurrences of old have been replaced with new, optionally restricting the number of replacements to max

Syntax:

str.replace(old, new[, max])

Parameters:

Here is the detail of parameters:

  • old: old substring to be replaced.

  • new: new substring which would replace old substring.

  • max: if the optional argument max is given, only the first count occurrences are replaced.

Return Value:

It returns a copy of the string with all occurrences of substring old replaced by new. If the optional argument max is given, only the first count occurrences are replaced.

Example:

#!/usr/bin/python

str = "this is string example....wow!!! this is really string";
print str.replace("is", "was");
print str.replace("is", "was", 3);

This will produce following result:

thwas was string example....wow!!! thwas was really string
thwas was string example....wow!!! thwas is really string


previous next Printer Friendly



  

Advertisement

Online Image Processing

Indian Baby Names