This paper analyzes the Python string connection method. Share to everyone for your reference, as follows:
Python string Connection There are several methods, the list that everyone may use, the first method is the lowest efficiency, and also to introduce the following 2 kinds of high-efficiency methods, we hope to help.
First introduced lower efficiency, some novice friends will make this mistake:
A = [' A ', ' B ', ' C ', ' d ']content = ' For i in a: content = content + iprint content
Why is efficiency low?
Cause: In the loop connection string, he once each connection, will re-open space, and then connect the string, then put in new space, once again cycle, but also open up new space, the string connected to put into the new space, so repeated, memory operations more frequent, each time to calculate memory space, Then open up memory space, and then free up memory space, the efficiency is very low, you may be less than the operation of the data can not be seen, the feeling of little impact, but you encounter more than the amount of operation data, this method will retire.
Or take a look at the following 2 more advanced methods to put.
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%s%s '% tuple (a) print content
More readers interested in Python related content can view this site topic: "Introduction to Python and Advanced Classic Tutorial" and "Python file and directory Operations Tips Summary"
I hope this article is helpful for Python program design.