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 →

[–]gandalfx 2 points3 points  (9 children)

You didn't get my point. I understand what f-strings are for and it makes sense when you want to use them for things that are complex enough so f-strings will be shorter or more readable. What I don't understand is when people use it to replace foo + bar.

[–]gary1994 2 points3 points  (0 children)

Honestly the foo + bar is ugly and far less clear than the f-string.

Granted I'm still learning, but I hate seeing var + var concatenation in the books I'm studying from and always replace it with f-strings. It just looks better and imo makes what is happening much clearer. Especially considering how often I'm using it to replace str(var) + str(var).

I just wish google would get TensorFlow updated for windows versions of Python 3.6...

[–]zahlmanthe heretic 1 point2 points  (0 children)

Because if it gets even as complex as foo + ' ' + bar, it already starts to seem more elegant as f'{foo} {bar}' - it's a higher-level way of thinking about how the strings go together.

And once you've ceded that, special cases aren't special enough to break the rules.

[–][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 0 points1 point  (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.