Replace String in Python

Often you’ll have a string (str object), where you will want to modify the contents by replacing one piece of text with another. In Python, everything is an object – including strings. This includes the str object. Luckily, Python’s string module comes with a replace() method. The replace() method is part of the string module, and can be called either from a str object or from the string module alone.

Python’s string.replace() Prototype

The prototype of the string.replace() method is as follows:

Python

1

string.replace(s,old,new[,maxreplace])

Function parameters

s: The string to search and replace from.

old: The old sub-string you wish to replace.

new: The new sub-string you wish to put in-place of the old one.

maxreplace: The maximum number of times you wish to replace the sub-string.

POPULAR CATEGORY

Tech Justice is a Security News Platform that centers around Infosec, Hacking, Xero-days, Malware, Vulnerabilities,Cyber Crime, DDoS, Surveillance, Privacy Issues,Breaking technology news and analysis The number one guide for all things tech and to keep you Informed and Secure