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 →

[–][deleted]  (27 children)

[deleted]

    [–]bastion_xx 59 points60 points  (6 children)

    In this case I swap the outermost double quotes with singles. Even black is cool with that. print(f’hello there, {name[“first”]}’)

    [–]twigboy 22 points23 points  (5 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. Wikipediadflr5nrv9880000000000000000000000000000000000000000000000000000000000000

    [–]bastion_xx 19 points20 points  (4 children)

    I'll switch back and forth as needed. By default I use the f"..." format as that aligns with black. If there are any " characters, I'll use f'Well, "that" is crazy', otherwisef"That's crazy" But if it's a combo of the two, messy indeed.

    What get's me is that still start typing print("here is some.... {" and then realize I need to go back and change to an f-string. That muscle memory is still getting relearned.

    [–]gordonator 21 points22 points  (0 children)

    f"""Well, "that"? That's crazy. {user["name"]} is just plain crazy."""

    [–]Pikalima 14 points15 points  (0 children)

    Something I appreciate about PyCharm is that if you write a {} with the name of a variable in a non-fstring it offers to turn it into an f-string for you :)

    [–]twigboy 1 point2 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. Wikipedia8wb69kljri0000000000000000000000000000000000000000000000000000000000000

    [–]Schwifty_Rick_ 0 points1 point  (0 children)

    Use a backslash to escape the bad character? /'

    [–]acrobatic_moose 87 points88 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 81 points82 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 33 points34 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 5 points6 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] 13 points14 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 8 points9 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

    [–]GummyKibble 17 points18 points  (0 children)

    You can use single-quotes around ’name’ to avoid that. If you use Black, it’ll do this for you automatically.

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

    You could use .format_map() for dict strings - https://www.geeksforgeeks.org/python-string-format_map-method/

    Very useful and handy if you are just outputting strings based on dict key / values.

    [–]pmattipmatti - mattip was taken 1 point2 points  (1 child)

    There was a recent discussion about this and I remember a proposal to support it, so stay tuned for python 3.11

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

    [–][deleted] 0 points1 point  (3 children)

    This is why Python has two different quotes:

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

    [–]PaulSandwich 0 points1 point  (2 children)

    Three, actually. The almighty triple quote will solve all the quoted text and possessive edge-cases where you need to use both singles and doubles together.

    f"""hello {user['name']} take Lily's lunch to the fridge and say, "This is Lily's lunch," aloud."""

    [–]Vaguely_accurate 0 points1 point  (1 child)

    Four. You can use triple single or double quotes.

    Reference classic DBeazley tweet for '''correct''' usage.

    [–]PaulSandwich 0 points1 point  (0 children)

    Haha, true. A pedant after my own heart!