What is the usage of replace in python?

烟雨青岚
Release: 2020-06-17 09:27:10
Original
11353 people have browsed it

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);
Copy after login

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
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!