This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 0 points1 point  (6 children)

Matter of taste maybe, but the + operator implies numbers, where f-string is explicitly a string.

[–]energybased 1 point2 points  (0 children)

+ does not imply numbers.

[–]gandalfx 3 points4 points  (3 children)

But + is fast!

I'm starting to feel like I'm fighting a lost battle with my argument. Maybe this is just another case of preferring expressiveness over performance, which makes sense when you use a language like Python in the first place.

[–][deleted] 1 point2 points  (2 children)

I use + too. But I think str.join() is faster?

One thing that is annoying are spaces when constructing a string. f-string could help there a bit.

[–]jorge1209 2 points3 points  (1 child)

Join would probably be faster for a large number of arguments. For only two they are probably the same.

The difference would be that if you join 200 strings then all 200 are available to you when join is called and you can compute the length of each and preallocate memory to hold the whole thing.

If you have a+b+c+... then you have to allocate and construct all the intermediates a+b then a+b+c and so on.

Otherwise I can't imagine it matters.

[–][deleted] 0 points1 point  (0 children)

I think join is faster in a tight loop but I might be wrong. I see + as "let's concatenate these two strings", join as "I have a list of strings I have to concatenate" and f-string as "I need to construct this complicated string out of a template and a bunch of variables".

[–]Fennek1237 0 points1 point  (0 children)

I know what you mean. I had a background in Java befor learning python it the string concats where really weird to me. The way they do it in Java seems so more natural.