0

I am having some trouble with aligning things in my output. Down below I have copied the expected output and the output my code gives me. It is very similar, however, the numbers aren't aligned correctly. My numbers would align to the right (and this first column can only be as long as the longest number in this column). What am I forgetting in my code?

Expected:

 1 aggc tgtc aatg
13 ctag gcat agaa
25 gtcg tgct gtag
37 agat agtc tgat
49 agtc gc

Got:

1 aggc tgtc aatg
13 ctag gcat agaa
25 gtcg tgct gtag
37 agat agtc tgat
49 agtc gc

Here is the code in which this problem occurs. I have left out some lines for simplicity, denoted by (...):

for block in blocks:
    (...)
        to_return += f'{str(index_row): >} {block_row}\n'

# print our final line after iterating through all the blocks
(...)
to_return += f'{str(index_row): >} {block_row}'
return to_return

I thought that using an f-string with the ">" sign would align my numbers to the right. My guess is that I need a number after the ">" sign. The thing is, I don't know beforehand which number, since the width of this first column should be as long as the largest number in my output (and I don't know how long this number will be).

Thank you for any help and tips!

EDIT: My guess is I will need to use a number after the > sign. Since I will have to calculate this number and store it in a variable, can I use a variable name instead of a number, like this:

to_return += f'{str(index_row):>length} {block_row}\n'

This line gives a Value error. Is there a way this can work?

3
  • 1
    You can either check what the largest number will be and use its length; or you can use an arbitrary number like 8 or something, based on your estimate of the largest number you'll need to print Commented Jan 6, 2022 at 9:44
  • @JiříBaum Thank you for your help. Do I need to use a number here, or can I also use a variable (e.g. length) referring to a number? I tried it in the Python console but referring to a variable doesn't seem to work. This is what I tried: to_return += f'{str(index_row):>length} {block_row}\n' Commented Jan 7, 2022 at 7:24
  • I think there's a special syntax for getting the length from a variable, but I'd have to look it up in the manual Commented Jan 7, 2022 at 9:05

2 Answers 2

2

Once you got the length of the largest number in a variable, you need to wrap it in {} like this:

to_return += f"{index_row:>{length}} {block_row}\n"

Example:

blocks = [(1, "aggc"), (13, "tgtc"), (135, "aatg")]
length = 3
to_return = ""
for index_row, block_row in blocks:
    to_return += f"{index_row:>{length}} {block_row}\n"
print(to_return.rstrip())

Example output:

  1 aggc
 13 tgtc
135 aatg
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much! By using your code I got to solve the exercise I was working on for my programming course. Your answer was clear, simple and it worked. I appreciate your help.
0

You need to add number after > to indicate length. Try:

f'{index_row: >2} {block_row}'

So:

for block in blocks:
    (...)
        to_return += f'{str(index_row): >2} {block_row}\n'

# print our final line after iterating through all the blocks
(...)
to_return += f'{str(index_row): >2} {block_row}'
return to_return

1 Comment

I know that adding a number would solve the problem, but the thing is I don't know this number. I calculated the length of the largest number and stored it in a variable length. This should be the width of this first column in the output. But if I insert a variable name instead of the 2 in your example, I get a Value error... How can I avoid this?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.