Python's string.replace() Method - Replacing Python Strings | Python Central
source link: https://www.pythoncentral.io/pythons-string-replace-method-replacing-python-strings/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Replacing Python Strings
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:
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.
Examples
From the string Module Directly
This gives us the following output:
And using the string.replace()
method from the str
object:
Which gives us:
Shockingly, we get the same output.
And there you have it! Python's string.replace()
.
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK