r/ProgrammerHumor • • Aug 26 '20

Python goes brrrr

Post image
59.2k Upvotes

789 comments sorted by

View all comments

797

u/geeshta Aug 26 '20

f"Python goes b{'r'*10}"

0

u/[deleted] Aug 26 '20

[deleted]

37

u/pterencephalon Aug 26 '20

I'm enjoying going through my old code and changing things to fstrings. Instead of actually fixing the real problems with the code.

13

u/jad2192 Aug 26 '20

Same, I go back through my old code and see tons of '%s %d' % (x, y) bullshit and wonder how I could have ever been such a uneducated swine.

2

u/BirkTheBrick Aug 26 '20

...please teach an educated swine who still does that? Lmao

4

u/jad2192 Aug 27 '20

With f-strings you can directly sub in variables without needing to declare the type. My above silly example would become f'{x}{y}' which doesn't seem much simpler but as the string to format gets more complicated it's nice not having keep track of the order of the variables at the end. In my job as a data scientist this made writing dynamic SQL queries for pipelines and dashboards SO much easier. Here is an article with a good run down

367

u/Jeb_Jenky Aug 26 '20

This made my nips so hard.

314

u/xDarkFlame25 Aug 26 '20

Ah yes fstrings, the ultimate fetish.

155

u/AinsleyBoy Aug 26 '20

I fucking love fstrings. I use them so much

42

u/Moldy_pirate Aug 26 '20

Is there ever a reason to use a “regular” string rather than an f”string?

78

u/GenericRedditor12345 Aug 26 '20

If you don’t need the functionality of an f string :p They’ve been optimized to be faster than the other formatting methods IIRC.

22

u/thirdegree Violet security clearance Aug 26 '20

Logging, there you want to do like:

logging.info("value1: %s, value2: %s", 1, 2)

This is because the formatting is only done if the log line will actually be emitted. It can be a significant performance boost if you have a lot of logging.

5

u/[deleted] Aug 26 '20

You can always have a lazy evaluation, using str::format, i. e. log("python goes b{0}", 'r'*10). The string would only format if it were to be used.

→ More replies (3)
→ More replies (1)

2

u/HTTP_404_NotFound Aug 26 '20

In .net, its called string interpolation.

Its one of our favorite features too.

Var x = $"Hello {fuser.name}";

2

u/al_at_work Aug 26 '20

I've got a few cases where I need to use `str.format` because the input string isn't something I can define locally.

Also, I don't have to deal with this, but if you're doing i18n there's no good way to use f-strings.

2

u/[deleted] Aug 26 '20

Sometimes I like to have a template string set up with parameters and then later on call template_string.format(**parameter_dict)

2

u/Hippemann Aug 27 '20

This edge case that happened to me: you have dict which has a key that contains a backslash

> d = {"\n" : "foo", "a":"bar"}

> f"{d['a']}"

 # "bar

 > f"{d['\n']}"

 > SyntaxError: f-string expression part cannot include a backslash

I had to use string concatenation

As I said this is an edge case and you will most likely never encounter it

→ More replies (8)

2

u/[deleted] Sep 26 '20

Fuckstrings, because I want to make sweet love to them.

→ More replies (2)

2

u/VerneAsimov Aug 26 '20

F-string sounds like lingerie. Perfect because it has curves {} and it's very sexy.

2

u/vigilantcomicpenguin Aug 26 '20

I don’t know what fstrings are but I’m turned on.

12

u/slix_88 Aug 26 '20

Fstrings in chat bois

23

u/marmoshet Aug 26 '20

laughs in 3.6

3

u/mxzf Aug 26 '20

You still have .format(), even in 2.x. Which is still way better than formatting strings with %.

8

u/[deleted] Aug 26 '20

Oh, fuck yeah, Fstrings. Grip it and rip it, dude!

→ More replies (2)
→ More replies (2)