Home >Backend Development >Python Tutorial >What is the usage of replace in python?

What is the usage of replace in python?

烟雨青岚
烟雨青岚Original
2020-06-17 09:27:1011467browse

What is the usage of replace in python?

#What is the usage of replace in python?

Python replace() method replaces old (old string) in the string with new (new string). If the third parameter max is specified, replace No more than max times.

replace() method syntax:

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

Parameters:

old -- the substring to be replaced. new – new string, used to replace old substring. max -- Optional string, replaced no more than max times

Return value:

Return the old (old string) in the string and replace it with new (new string), if the third parameter max is specified, the replacement will not exceed max times.

The following example shows how to use the replace() function:

#!/usr/bin/python
str = "this is string example....wow!!! this is really string";
print str.replace("is", "was");
print str.replace("is", "was", 3);

The output result of the above example is as follows:

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

Recommended tutorial: "python tutorial"

The above is the detailed content of What is the usage of replace in python?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn