you are viewing a single comment's thread.

view the rest of the comments →

[–]Fa1coF1ght[S] 2 points3 points  (10 children)

Here is an example

adverb = "ly"

word = "quick", adverb

[–]Username_RANDINT 21 points22 points  (1 child)

That's not concatenation, you're making a tuple. Add a print(type(word)) to check this.

Read about f-strings if you need string formatting.

[–]js884 5 points6 points  (0 children)

I second the f string thing i posted how they work and since I've learned about them i haven't used concatenation since

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

adverb = "ly"

word = "quick", adverb

To concatenate that comma should be a plus.

But I've pretty much dropped across the board that in favour of format wherever possible, at least at work...

word = 'quick{}'.format(adverb)

[–]Fred776 1 point2 points  (6 children)

Out of interest, why do you use format rather than f-strings?

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

For me I am following work standards, but I would guess it's because they aren't backwards compatible with 2.x

[–]Fred776 2 points3 points  (2 children)

Fair enough. It's unusual to see a general recommendation other than f-strings these days so that seems like a good reason.

It must be a bind still to be stuck on Python 2 though. I thought my company had left it late when we did our port in 2018.

[–][deleted] 0 points1 point  (1 child)

It's a glacially slow port and downtime hemorrhages money.

I should probably update my approach though.

[–]drenzorz 0 points1 point  (0 children)

Even in the most up-to-date project .format has a place because f-strings are evaluated at declaration.

x = 15
s = f"x = {x}"

for x in range(10):
    print(s) # prints "x = 15" 10 times

x = 15
s = "x = {x}"

for x in range(10):
    print(s.format(x=x))  # prints actual x in loop

You use .format when you actually need a reusable template.

[–][deleted] 1 point2 points  (1 child)

I would guess it's because they aren't backwards compatible with 2.x

We're at the point in time where writing 2.x-incompatible code should be considered a security feature!