I am reading The Hitchhiker’s Guide to Python and there is a short code snippet
foo = 'foo' bar = 'bar' foobar = foo + bar # This is good foo += 'ooo' # This is bad, instead you should do: foo = ''.join([foo, 'ooo'])
The author pointed out that ''.join()
is not always faster than +
, so he is not against using +
for string concatenation.
But why is foo += 'ooo'
bad practice whereas foobar=foo+bar
is considered good?
foo += bar
good?foo = foo + 'ooo'
good?Before this code snippet, the author wrote:
One final thing to mention about strings is that using join() is not always best. In the instances where you are creating a new string from a pre-determined number of strings, using the addition operator is actually faster, but in cases like above or in cases where you are adding to an existing string, using join() should be your preferred method.
Due to this, mixing the StringBuilder and + method of concatenation is considered bad practice. Additionally, String concatenation using the + operator within a loop should be avoided. Since the String object is immutable, each call for concatenation will result in a new String object being created.
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.
From a readability standpoint, f-string literals are more aesthetically pleasing and easier to read than string concatenation.
It violates the rules of how ID values and += are supposed to work - the ID values produced with the optimization in place would be not only impossible, but prohibited, with the unoptimized semantics - but the developers care more about people who would see bad concatenation performance and assume Python sucks.
It's reasonable to assume that it isn't bad practice for this example because:
foo += 'ooo'
is just as readable (according to me) and is approximately 100 times faster than foo = ''.join([foo, 'ooo'])
.Concatenation of strings have the disadvantage of needing to create a new string and allocate new memory for every concatenation! This is time consuming, but isn't that big of a deal with few and small strings. When you know the number of strings to concatenate and don't need more than maybe 2-4 concatenations I'd go for it.
When joining strings Python only has to allocate new memory for the final string, which is much more efficient, but could take longer to compute. Also, because strings are immutable it's often more practical to use a list of strings to dynamically mutate, and only convert it to a string when needed.
It's often convenient to create strings with str.join() since it takes an iterable. For example:
letters = ", ".join("abcdefghij")
In most cases it makes more sense to use str.join()
but there are times when concatenation is just as viable. Using any form of string concatenation for huge or many strings would be bad practice just as using str.join()
would be bad practice for short and few strings, in my own opinion.
I believe that the author was just trying to create a rule of thumb to easier identify when to use what without going in too much detail or make it complicated.
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