Python Replace () method

Source: Internet
Author: User
Describe

The Python replace () method replaces the old (older string) in the string with the new one, and if you specify the third parameter max, the replacement does not exceed Max times.

Grammar

Replace () method syntax:

Str.replace (old, new[, Max])
Parameters
  • Old--the substring to be replaced.
  • New-A string that replaces the old substring.
  • Max--Optional string, replace no more than Max Times
return value

Returns a new string that is generated after the old (older string) in the string is replaced with new (the newly created string), and if the third parameter max is specified, the substitution does not exceed Max times.

Instance

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

#!/usr/bin/pythonstr = "This is a string EXAMPLE....WOW!!! This is really string ";p rint str.replace (' is ', ' was ');p rint str.replace (" is "," was ", 3);

The result of the above example output is as follows:

Thwas was string Example....wow!!! Thwas was really stringthwas is string EXAMPLE....WOW!!! Thwas is really string

Transferred from: http://www.runoob.com/python/att-string-replace.html
Recently began to learn Python, wrote an XSS scanner, encountered a lot of problems, in the following days, I will put these insights and read the information written or loaded into the blog, just as a way to consult later.
Learn a little every day!

Python Replace () method

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.