This article mainly introduces the python string connection method, compares and analyzes common string connection techniques in the form of instances, and has some reference value, you can refer to the following example to analyze 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