Example analysis of Replace method in Python _python

Source: Internet
Author: User

This article describes the Replace method in Python in the form of an instance, which is very useful, as follows:

There are two main types of replace methods:

Last_date = "1/2/3" target is "123"

Method One: Repalce

Date =last_date.replace ('/', ')

Method Two: RE

p = re.compile ("/")
date = P.sub (", last_date)

Note that you must not escape, or the function will not take effect.

The Replace method returns a copy of the string that is replaced with text based on the regular expression, in the following format:

Stringobj.replace (Rgexp, ReplaceText)

Parameters:

Stringobj required Option . The string object or string literal to perform the substitution. The string is not modified by the Replace method.
Rgexp required Option . Is the regular expression object that contains the regular expression pattern or the available flags. It can also be a String object or text. If Rgexp is not a regular expression object, it is converted to a string and the exact lookup is made; do not attempt to convert the string to a regular expression.
ReplaceText required Option . is a string object or string literal, and the position in each matching rgexp in Stringobj is replaced with the text contained in that object. In the Jscript 5.5 or later version, the ReplaceText parameter can also be a function that returns the replacement text.

For example:

Content.replace (' \ n ', '). Replace (' \ R ', '). replace (' \ \ ', ' \\\\ '). Replace (', ', ', ')

It is hoped that the examples mentioned in this paper will play a reference role in Python programming.

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.