Python f-strings > Dutch government

Python f-strings > Dutch government

31 comments · 83% upvoted · source · other discussions

comments

Compux72

Yep that’s unreadable

BaalKazar

percentage times ten, min(result, 10)

(x) times result

(0) fill until result gets 10 symbols long

Pretty readable imo, I don’t even know/do python

xaomaw

What about :⚪<10

What does the : do? How can you compare ⚪ to 10?

Arny597

That’s the “fill result until 10 symbols long” part

xaomaw

Okay so I got the answer. The colon : itself stands for an if-statement. That's what my first question was about.

But even then the comparison is

if ⚪ < 10:
    🔵 * count_of_blue_dots

So I still have the question how can you compare ⚪ < 10? I would understand if ⚪ was defined as a variable containing the count_of_white_dots before, but it is NOT.

Arny597

How do you know it’s an if statement when colons have an entirely different function in python as well? This link tells you how colon operators are used for string slicing. So maybe it’s formatting the blue dot to be as many times as the percentage, then filling the string with white dots as long as the string is less than 10 elements long

EDIT: not only that, but when colons ARE used in an if statement, it’s never to replace the words. The ternary operators “?” And “:” which mean “if” and “else” respectively do not exist in python. Shorthand if statements always use the keywords still, such as “a if b else c”

Synedh

It's not an if-statement, it's a format delimiter.

In python, you can format your in-string argument this way : f"Foo {val:format}" where format is a mini-language by itself. It is a very powerful and an efficient way to deal with strings formating.

xaomaw

Thanks for clarification, I'll take a look into it!

Edit:

<   Forces the field to be left-aligned within the available space (this is the default for most objects).

Now I understand it. Very cool

Synedh

Indeed, very useful for unit or date format, if you need your time on two chars, a simple f"{hour:0<2}:{min:0<2}".

Swoop3dp

Basically left-pad but without the drama.

00PT

It looks like one of those obscure syntax elements that is convenient, powerful, and makes sense, but not at all intuitively understood unless you are specifically told how it works or look it up. There seems to be many of these in Python. It's not good for widespread readability.

BaalKazar

Honestly I actually don’t know what the syntax exactly did.

The curly braces with such a comparison made it look like a loop to me. Then I imagined Python doing Python things in which comparing the string against an integer somehow compares the length of the string.

I can imagine automatic refactoring coming up with stuff luck this, I don’t know how alien that syntax is to python bases though, but the case it self is quirky enough