This article describes 3 ways to combine Python strings, and this article explains the use of the + + operator, using the% operator, and using String's '. Join () Method 3 methods, need friends can refer to the following
Objective
Combine some small strings into a large string, with more consideration for performance
Method
There are several common ways to do this:
1. Use the + = operator
The code is as follows:
Bigstring=small1+small2+small3+...+smalln
For example there is a fragment pieces=[' Today ', ' are ', ' really ', ' a ', ' good ', ' Day '], we want to link it up
The code is as follows:
Bigstring= '
For E in pieces:
Bigstring+=e+ '
or use
The code is as follows:
Import operator
Bigstring=reduce (Operator.add,pieces, "")
2. Use% operator
The code is as follows:
In [per]: print '%s,your current%.1f '% (' Nupta ', 500.52)
Nupta,your is 500.5
3. Use string's '. Join () method
The code is as follows:
In [%]: '. Join (Pieces)
OUT[34]: ' Is really a good day '
About Performance
A small number of strings need to be spliced to use the% operator to keep the code readable
There are a lot of strings that need stitching, using the '. Join method, which uses only one pieces copy without having to produce intermediate results between the subkeys.