Similar questions have been brought (good speed comparison there) on this same subject. Hopefully this question is different and updated to Python 2.6 and 3.0.
So far I believe the faster and most compatible method (among different Python versions) is the plain simple +
sign:
text = "whatever" + " you " + SAY
But I keep hearing and reading it's not secure and / or advisable.
I'm not even sure how many methods are there to manipulate strings! I could count only about 4: There's interpolation and all its sub-options such as %
and format
and then there's the simple ones, join
and +
.
Finally, the new approach to string formatting, which is with format
, is certainly not good for backwards compatibility at same time making %
not good for forward compatibility. But should it be used for every string manipulation, including every concatenation, whenever we restrict ourselves to 3.x only?
Well, maybe this is more of a wiki than a question, but I do wish to have an answer on which is the proper usage of each string manipulation method. And which one could be generally used with each focus in mind (best all around for compatibility, for speed and for security).
Thanks.
edit: I'm not sure I should accept an answer if I don't feel it really answers the question... But my point is that all them 3 together do a proper job.
Daniel's most voted answer is actually the one I'd prefer for accepting, if not for the "note". I highly disagree with "concatenation is strictly using the + operator to concatenate strings" because, for one, join
does string concatenation as well, and we can build any arbitrary library for that.
All current 3 answers are valuable and I'd rather having some answer mixing them all. While nobody volunteer to do that, I guess by choosing the one less voted (but fairly broader than THC4k's, which is more like a large and very welcomed comment) I can draw attention to the others as well.
One of the most popular methods to concatenate two strings in Python (or more) is using the + operator. The + operator, when used with two strings, concatenates the strings together to form one.
The main advantages of using format(…) are that the string can be a bit easier to produce and read as in particular in the second example, and that we don't have to explicitly convert all non-string variables to strings with str(…).
This behaviour should not surprise you: Python's string objects are immutable, so each concatenation generates a new string instead of modifying the existing one in place. It's also no surprise that multiple string concatenations are considered slow.
As a note: Really this is all about string construction and not concatenation, per se, as concatenation is strictly using the +
operator to concatenate strings together one after the other.
+
(concatenation) - generally inefficient but can be easier to read for some people, only use when readability is priority and performance is not (simple scripts, throwaway scripts, non-performance intensive code)join
(building a string from a sequence of strings) - use this when you have a sequence of strings that you need to join using a common character (or no character at all if you want to use the empty string ''
to join on)%
and format
(interpolation) - basically every other operation should use whichever one of these is appropriate, choose which operator/function is appropriate based on which version of Python you want to support for the lifetime of the code (use %
for 2.x and format
for 3.x)If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With