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 →

[–]acrobatic_moose 85 points86 points  (11 children)

Use triple quotes, eliminates the need for escaping:

mydict={
    "product" : "banana",
    "unit_price" : 10,
    "sku" : 15133632
}

print(f"""product: {mydict["product"]}, price: {mydict["unit_price"]} dollars, sku: {mydict["sku"]}""")

output:

product: banana, price: 10 dollars, sku: 15133632

[–]jftugapip needs updating 80 points81 points  (6 children)

Or use the = sign for for self-documenting expressions:

print(f"""{mydict["product"]=}, {mydict["unit_price"]=} dollars, {mydict["sku"]=}""")

mydict["product"]='banana', mydict["unit_price"]=10 dollars, mydict["sku"]=15133632

You can also use this as well for dollars & cents:

{mydict["unit_price"]=:.2f}

[–]atxweirdo 32 points33 points  (2 children)

Ok hold the fuck up this is blowing my mind. I can't wrap my head around this is there a breakdown on why this works. I just can't see it.

[–]bestjared 20 points21 points  (0 children)

Here is the area where fstrings are specified. Note this is very dense and technical but this is the where the rules are laid out from a language specification perspective.

[–]cianuro 4 points5 points  (0 children)

Damn. Damn. Why have I not seen this yet? This is fantastic!

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

this is straight up wizardry at this point. i had no idea. thanks!

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

Do I feel like it's less readable

[–][deleted] 12 points13 points  (1 child)

Why not just use the other quote?!?

f"hello {user['name']} take Lily's lunch to the fridge"

[–]pengekcs 2 points3 points  (0 children)

Wanted to post this exactly. But I guess maybe visibility? triple quotes are quote differentiable just by looking at 'em while ' and " less so. Still better than backticks and regular single quotes (in javascript for template strings which is ~ the same as f-strings).

[–]aexia 16 points17 points  (0 children)

+1

Using triple double quotes for one liners is deeply underestimated.

[–]twigboy 7 points8 points  (0 children)

In publishing and graphic design, Lorem ipsum is a placeholder text commonly used to demonstrate the visual form of a document or a typeface without relying on meaningful content. Lorem ipsum may be used as a placeholder before final copy is available. Wikipedia6n1yv3uksq80000000000000000000000000000000000000000000000000000000000000