# String size control

Hello, Back to an old problem. if you remember:
‘’’
for n in range(0, items, 1):
print(“\t\t”, list1[n], end=“\t\t\t\t “)
print(”{:.2f}\t{}”.format(float(list1[n]) * ratio, list2[n]))
‘’’
this prints: int() Float() ItemName
Now, I want: ItemName int(] Float()
The (next) problem is if item is too long, it pushes int()
and Float() to the right out of format with the rest of the lines
Here is what I do NOT want:

oats 1 2 cups
watermelons 1 2 cups < NONONO!
figs 1 2 cups
ice 1 2 cups

I tried moving list2[n] around, but did not work. There’s a way; I did for a class project in Basic. I Long time ago! Definitely do NOT remember the math. But, I do remember it took me days to figure it out! Any newer Python solutions are appreciated

If it were me, I’d maybe generate a grid of results (a list of lust of
str), then measure the width of all the strings. Make each column as
wide as its iwdest string. Allow one extra space for separation.

Then print the whole grid, suitably padded according to your
measurements.

A common approach (not as thorough as the above, and suited to
progressive output where you don’t really have the opportunity to
measure the whole grid, for whatever reason) is to pick some suitable
widths for the common “wide” widths, always print the separating space,
and just accept that really wide column values push things to the right.
You can somewhat accomodate the drift by noting when a column gets too
whatever excess was used by the very wide value.

This approach has the advantage that you don’t need to defer printing
until later but still keeps a space between values to aid parsing the
output and to prevent wide values abutting the following value.

Cheers,
Cameron Simpson cs@cskk.id.au

1 Like

You’ve demonstrated what you don’t want, but what is it that you do want?

This?

``````oats        1 2 cups
watermelons 1 2 cups
figs        1 2 cups
ice         1 2 cups
``````

If so, then you can format the space that the ‘ingredients’ occupies, based on the longest. The only other option that I can think of is to truncate the ‘ingredients’, but you may end off with something like:

``````oats 1 2 cups
wate 1 2 cups
figs 1 2 cups
ice  1 2 cups
``````

Or, maybe I’m missing something here? In which case, post your code (formatted please) so that it’s clear as to where you’re at.

for n in range(0, items, 1):
-----print(“\t\t”, list1[n], end=“\t\t\t\t “)
-----print(”{:.2f}\t{}”.format(float(list1[n]) * ratio, list2[n]))

Thanks for your reply. It’s hidden up there in my bad formatting. Let me try that again
In the variables above, cups and oats are the same list items (list2[n])
and the numbers are list1[n]. [n] is the number of items in the recipe and is the items in the loop that controls everything else. You saw what I’m getting which I don’t want…to answer your question, This output would make me happy, all the digits are lined up.
boxess oats------------ 2--------4
cups watermelon ----- 2--------4
cups figs ---------------- 2--------4
No matter how long the item in list2[n] is, it will still play nice with the others. The “-” are spaces, no time to figure out how to do spaces here. Sorry No truncating (ugly). Thanks again! I’check tomorrow… Good night.

You’re welcome.

Maybe there’s something here that I’m still missing, but as a POC, this script:

``````x = ["oats", "watermelons", "figs", "ice"]

for item in x:
``````

… produces this output:

``````oats        other stuff
watermelons other stuff
figs        other stuff
ice         other stuff
``````

Does that help?

To add: have a read of this guide so that you know how to post formatted code and such like.

Thanks for your attempt and for the guide. I’ve spent enough time on this. Time to move on. Thanks again.

No worries.
Some general tips:

1. No need to have cryptic names for Python objects: `list1` means nothing. `list2` means nothing. Give your Python objects meaningful names.

2. As above: `n` means nothing. If `n` is a number, then call it `num`, least ways, so that the reader has a clue, esp when there is no other code to reference.

3. Posting a non-working code snippet simply leaves the reader to guess at what is missing.

4. ‘Moving the goal posts’ (changing the objective) as you have done by posting two different layouts for the output, simply adds to the confusion.

5. Markdown is not hard to learn; in fact it’s easier to learn than Python and makes for a far better and easier to read posting.

6. Never give up! ← That one is for life in general, not just for coding.