Python string connection method analysis, python string connection

Source: Internet
Author: User

Python string connection method analysis, python string connection

This article analyzes the python string connection method. We will share this with you for your reference. The details are as follows:

There are several methods to connect python strings and list the methods that you may use. The first method has the lowest efficiency. In addition, we will introduce the following two efficient methods, I hope to help you.

First, we will introduce the low efficiency. Some new friends will make this mistake:

A = ['A', 'B', 'C', 'D'] content = ''for I in a: content = content + iprint content

Why is efficiency low?

Cause: When cyclically concatenates a string, each time the string is connected, it is necessary to re-open the space, then connect the string, put it into a new space, and then loop again, it also needs to open up new spaces and connect strings into new spaces. This is repeated and memory operations are frequent. Every time the memory space needs to be calculated, the memory space should be opened up, and then the memory space should be released, the efficiency is very low. You may not be able to see the data when you operate a small amount of data, but this method is about to retire when you encounter a large amount of operation data.

Let's take a look at the following two advanced methods.

Method 1: Use the string join method:

A = ['A', 'B', 'C', 'D'] content = ''content = ''. join (a) print content

Method 2: replace the placeholder with a string

A = ['A', 'B', 'C', 'D '] content = ''content =' % s % s' % tuple () print content

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.