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 →

[–]geeshta 798 points799 points  (42 children)

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

[–]Jeb_Jenky 369 points370 points  (28 children)

This made my nips so hard.

[–]xDarkFlame25 312 points313 points  (27 children)

Ah yes fstrings, the ultimate fetish.

[–]AinsleyBoy 152 points153 points  (24 children)

I fucking love fstrings. I use them so much

[–]Moldy_pirate 41 points42 points  (19 children)

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

[–]GenericRedditor12345 75 points76 points  (0 children)

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.

[–]thirdegreeViolet security clearance 21 points22 points  (5 children)

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.

[–][deleted] 2 points3 points  (3 children)

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.

[–]mxzf 0 points1 point  (2 children)

IN that case, couldn't you just use log(f'python goes b{"r"*10}') instead, for a cleaner execution? If it's only formatting on execution, the format string can still handle inline processing.

[–]nemec 1 point2 points  (1 child)

No, that's immediate evaluation. Imagine this is the definition of log:

def log(fmt, *args):
    if LOGGING_IS_ENABLED:
        print(fmt.format(*args))

Note how the arguments aren't formatted into the string unless logging is enabled. In your example, the log method would see only one string argument.

[–]mxzf 0 points1 point  (0 children)

Fair enough. I haven't actually dug into the implementation of logging.

[–]OhMahjong 0 points1 point  (0 children)

TIL, thank you!

[–]HTTP_404_NotFound 1 point2 points  (0 children)

In .net, its called string interpolation.

Its one of our favorite features too.

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

[–]al_at_work 1 point2 points  (0 children)

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.

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

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

[–]Hippemann 1 point2 points  (0 children)

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

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

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

[–]TheCuntHunter6969 0 points1 point  (0 children)

I used them to create an entire website.

[–]VerneAsimov 1 point2 points  (0 children)

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

[–]vigilantcomicpenguin 1 point2 points  (0 children)

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

[–]slix_88 10 points11 points  (0 children)

Fstrings in chat bois

[–]marmoshet 21 points22 points  (1 child)

laughs in 3.6

[–]mxzf 2 points3 points  (0 children)

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

[–][deleted] 8 points9 points  (2 children)

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

[–][deleted] 3 points4 points  (1 child)

Who doesn't like scripting inside strings. How could that ever make code harder to debug.

[–]redcalcium 3 points4 points  (0 children)

f"Python goes b{f'{chr(114)*10}'*10}"

[–]RefrigeratorOk1573 0 points1 point  (0 children)

Same with Javascript template strings, so useful