RE: Python Libraries: Simple... Display your Splinterlands DEC

avatar

You are viewing a single comment's thread:

I recommend looking at Python 'fstrings'. They make for simpler formatting. Here's an example I came up with that pads out names and numbers. Easier to read I think:

x=[{'name':'rod','amt':3},{'name':'jane','amt':555},{'name':'freddy','amt':111}]
for i in x:
    print(f"{i['name']:20} {i['amt']:6}")

Output:

rod                       3
jane                    555
freddy                  111


0
0
0.000
8 comments
avatar

More efficient for sure (1 line less of coding), but getting my head around it was tough. It does it for you! Here's the adjusted section of code:

image.png

image.png

Thanks for the lesson, I was never a master.., but can get by!

0
0
0.000
avatar

It's pretty powerful and generally quite readable. You can also align the numbers to the right. It's something I use all the time, but I don't know all the tricks.

0
0
0.000
avatar

Thanks for the insight, I'm not sure about the 'more readable' part but it's the way to go for sure. I always have to dig in and understand how things work.., having it simply work isn't enough.

0
0
0.000
avatar

It's something where you can quickly learn the basics and pick up further insight as you need it. You can get into really trying to optimise the amount of code, but that can compromise readability.

0
0
0.000
avatar

This is something I am struggling with.., given a variable name.. it works.. but suffixing the :20 to a literal string.. does not. I have tried several combinations.. which either gives me errors.. or prints the 20!

print(f'Account{:20} DEC Total')

Any ideas?

0
0
0.000
avatar

Try this. The part you are formatting has to be in {}. It can even be an expression. Anything outside the {} is output as-is.

print(f'{"Account":20} DEC Total')
0
0
0.000
avatar

Perfect and now working.. thanks!. So quotes can go inside the squiggles and are not interpreted as variable data if enclosed with quotes.. interesting. I couldn't find a single example of this on the net.

0
0
0.000
avatar

It can be expressions like this. Probably other ways to do it. Just need to use different quotes in the {} to those around the whole string.

for n in range(20,0,-1):
    print(f'{"*"*n:20} bottles of beer')
0
0
0.000