Use of the replace () method to operate strings in Python

Source: Internet
Author: User

Use of the replace () method to operate strings in Python

This article describes how to use the replace () method to operate strings in Python. It is a basic knowledge in Python. For more information, see

The replace () method returns the replace of the current old with new, which can be used to replace the copy of the maximum number of strings.

Syntax

The syntax of the replace () method is as follows:

?

1

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

Parameters

Old -- this is the old substring to be replaced.

New -- this is a new substring that replaces the old substring.

Max -- if the max value of this optional parameter is given, only the first count is replaced.

Return Value

This method returns the replace between the copy of the string and the old substring. If the maximum value of the optional parameter is specified, only the first count is replaced.

Example

The following example demonstrates the use of the replace () method.

?

1

2

3

4

5

#! /Usr/bin/python

 

Str = "this is string example... wow !!! This is really string ";

Print str. replace ("is", "was ");

Print str. replace ("is", "was", 3 );

When we run the above program, it will produce the following results:

?

1

2

Thwas was string example... wow !!! Thwas was really string

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

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.