Home > OS >  Python Formatting Text
Python Formatting Text

Time:11-03

I am trying to create a list of characters that fits within a certain area.

I have tried looking it up but could not find any help on this sort of formatting. The thing is, I need to be able to add/remove characters without messing up the formatting to the other names.

print("===================================================")
print("-     Character (heroes and villains) Summary     -")
print("===================================================")
print("-                              P  W  L  D  Health -")
print("---------------------------------------------------")
print("- ",character_list[0][0],"              ",character_list[0][3],"",character_list[0][4],"",character_list[0][5],"",character_list[0][6],"    ",character_list[0][7],"-")
print("---------------------------------------------------")
print("- ",character_list[1][0],"                    ",character_list[1][3],"",character_list[1][4],"",character_list[1][5],"",character_list[1][6],"    ",character_list[1][7],"-")
print("---------------------------------------------------")
print("- ",character_list[2][0],"                 ",character_list[2][3],"",character_list[2][4],"",character_list[2][5],"",character_list[2][6],"    ",character_list[2][7],"-")
print("---------------------------------------------------")
print("- ",character_list[3][0],"                  ",character_list[3][3],"",character_list[3][4],"",character_list[3][5],"",character_list[3][6],"   ",character_list[3][7],"-")
print("---------------------------------------------------")
print("- ",character_list[4][0],"                 ",character_list[4][3],"",character_list[4][4],"",character_list[4][5],"",character_list[4][6],"    ",character_list[4][7],"-")
print("---------------------------------------------------")
print("- ",character_list[5][0],"                   ",character_list[5][3],"",character_list[5][4],"",character_list[5][5],"",character_list[5][6],"    ",character_list[5][7],"-")
print("---------------------------------------------------")
print("- ",character_list[6][0],"                 ",character_list[6][3],"",character_list[6][4],"",character_list[6][5],"",character_list[6][6],"    ",character_list[6][7],"-")
print("---------------------------------------------------")
print("- ",character_list[7][0],"                      ",character_list[7][3],"",character_list[7][4],"",character_list[7][5],"",character_list[7][6],"    ",character_list[7][7],"-")
print("---------------------------------------------------")
print("- ",character_list[8][0],"                   ",character_list[8][3],"",character_list[8][4],"",character_list[8][5],"",character_list[8][6],"    ",character_list[8][7],"-")
print("---------------------------------------------------")
print("===================================================")

Output from the above code is as below

===================================================
-     Character (heroes and villains) Summary     -
===================================================
-                              P  W  L  D  Health -
---------------------------------------------------
-  Wonder Woman                5  5  0  0      90 -
---------------------------------------------------
-  Batman                      6  2  0  4      80 -
---------------------------------------------------
-  The Joker                   5  1  0  4      80 -
---------------------------------------------------
-  Superman                    7  4  0  3     100 -
---------------------------------------------------
-  Catwoman                   12  0  6  6      50 -
---------------------------------------------------
-  Aquaman                     8  2  2  4      30 -
---------------------------------------------------
-  Iron Man                   10  6  3  1      50 -
---------------------------------------------------
-  Hulk                        7  2  1  4      80 -
---------------------------------------------------
-  Thanos                     10  2  0  8      90 -
---------------------------------------------------
===================================================

The code is a little bit messy but it's what I've come up with for the moment that just works with the characters in the default code.

If anyone could help it would be greatly appreciated!

CodePudding user response:

looks like you have an array to store your characters, maybe try to store the output string of each character as well.

For example:

char_one = "- ",character_list[0][0],"              ",character_list[0][3],"",character_list[0][4],"",character_list[0][5],"",character_list[0][6],"    ",character_list[0][7],"-"
char_two = "- ",character_list[1][0],"              ",character_list[1][3],"",character_list[1][4],"",character_list[1][5],"",character_list[1][6],"    ",character_list[1][7],"-"

char_outputs = []
char_outputs.append(char_one)
char_outputs.append(char_two)

for out in char_outputs:
    print(out)
    print("---------------------------------------------------")

Then have a list to store the output of each character, the index of output should match your character in the character array.

use a for loop to print each character output string.

so if you remove a character, you can remove the corresponding output, and run for loop again, the format should keep the same.

CodePudding user response:

Try using a loop and .ljust(30," "). That will solve your formatting issues.

For example:

print("cheese".ljust(20," "))

prints the same width string as:

print("tea".ljust(20," "))

You can also use .rjust() on a string for the opposite padding.

CodePudding user response:

I think it would be much wiser to store your characters in dictionaries. Moreover, you seem to adjust padding between character name and "P" column manually. Use formatted strings in a loop instead:

character_list = [
    {
        "name": "Wonder Woman",
        "P": 5,
        "W": 5,
        "L": 0,
        "D": 0,
        "Health": 90
    },
    {
        "name": "Batman",
        "P": 6,
        "W": 2,
        "L": 0,
        "D": 4,
        "Health": 80
    },
    {
        "name": "The Joker",
        "P": 12,
        "W": 1,
        "L": 0,
        "D": 4,
        "Health": 80
    }
]


table_header = """===================================================
-     Character (heroes and villains) Summary     -
===================================================
-                              P  W  L  D  Health -
---------------------------------------------------"""

table_footer = """==================================================="""


print(table_header)
for char in character_list:
    print(f"- {char['name'].ljust(30 - len(str(char['P'])), ' ')}{char['P']}  {char['W']}  {char['L']}  {char['D']}      {char['Health']} -")
    print("---------------------------------------------------")
print(table_footer)

Output:

===================================================
-     Character (heroes and villains) Summary     -
===================================================
-                              P  W  L  D  Health -
---------------------------------------------------
- Wonder Woman                 5  5  0  0      90 -
---------------------------------------------------
- Batman                       6  2  0  4      80 -
---------------------------------------------------
- The Joker                   12  1  0  4      80 -
---------------------------------------------------
  • Related