2

I am trying to replicate this string:

enter image description here

This is what my attempt looks like, as you can see the values are not aligned correctly. I know I need to use some type of padding but everything I've done has failed.

enter image description here

Here is my code:

individual_text = '''
Highest Individual Question Scores
        •       Leaders
                •       {}{:.2f}
                •       {}{:.2f}
                •       {}{:.2f}
        •       Colleagues
                •       {}{:.2f}
                •       {}{:.2f}
                •       {}{:.2f}
Lowest Individual Question Scores
        •       Leaders
                •       {}{:.2f}
                •       {}{:.2f}
                •       {}{:.2f}
        •       Colleagues
                •       {}{:.2f}
                •       {}{:.2f}
                •       {}{:.2f}
'''.format(top_3_leader.index[0], top_3_leader.values[0],
           top_3_leader.index[1], top_3_leader.values[1],
           top_3_leader.index[2], top_3_leader.values[2],
           top_3_colleague.index[0], top_3_colleague.values[0],
           top_3_colleague.index[1], top_3_colleague.values[1], 
           top_3_colleague.index[2], top_3_colleague.values[2],
           bottom_3_leader.index[0], bottom_3_leader.values[0],
           bottom_3_leader.index[1], bottom_3_leader.values[1],
           bottom_3_leader.index[2], bottom_3_leader.values[2],
           bottom_3_colleague.index[0], bottom_3_colleague.values[0],
           bottom_3_colleague.index[1], bottom_3_colleague.values[1],
           bottom_3_colleague.index[2], bottom_3_colleague.values[2]
          )

How do I format my text to look like the 1st image?

2
  • 1
    I'd suggesting using \t Commented Apr 5, 2019 at 18:34
  • Maybe Texttable with no decorations? pypi.org/project/texttable Commented Apr 5, 2019 at 18:41

2 Answers 2

2

I think this is task for ljust method of str, consider following example:

x = [('text',1.14),('another text',7.96),('yet another text',9.53)]
for i in x:
    print(i[0].ljust(25)+"{:.2f}".format(i[1]))

Output:

text                     1.14
another text             7.96
yet another text         9.53

There also exist rjust adding space at beginning rather than end.

Sign up to request clarification or add additional context in comments.

Comments

1

You just need to specify one constant length for first element in each row:

individual_text = '''
                •       {:40}{:.2f}
                •       {:40}{:.2f}
'''.format('some_text', 3.86,
           'text_with_another_length', 3.85,
          )
print(individual_text)

# output:
#               •       some_text                               3.86
#               •       text_with_another_length                3.85

You can calculate this length in some way like this:

import itertools

minimum_spaces = 3
length = minimum_spaces + max(len(item) for item in itertools.chain(
    top_3_leader.index, top_3_colleague, bottom_3_leader, bottom_3_colleague
))

Comments

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.