Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Square display
#1
Hi all ! Here is a multiplication table. The first display is good but not square. The second display is square but plenty of apostrophes and commas, which I wish to delete. Can I have a square beautifull display ?? Tell me please what is a "python icon".  Thanks

# trial.py
from pprint import pprint

P = [[x * y for y in range(1, 10)] for x in range(1, 10)]
pprint(P)
for i in range(len(P)):
    for j in range(len(P)):
        if len(str(P[i][j])) == 1:
            P[i][j] = " " + str(P[i][j])
        else:
            P[i][j] = str(P[i][j])
for x in range(len(P)):
    print(P[x])
Reply
#2
There's probably more than one way to do this.
If you convert the list object to a string using join,
then you can get desired output:

from pprint import pprint

P = [[x * y for y in range(1, 10)] for x in range(1, 10)]
for i in range(len(P)):
    for j in range(len(P)):
        if len(str(P[i][j])) == 1:
            P[i][j] = " " + str(P[i][j])
        else:
            P[i][j] = str(P[i][j])
for x in range(len(P)):
   n = " ".join(P[x])
   print(n)
For some reason the webpage is formatting my terminal
output. Running on the linux terminal the output is
sqaure with a padded space, which is removed on the
web page.
Reply
#3
Yes. Your code works very well with my linux. Only numbers displayed in a square. Thank you very much.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Im at square one even with trying to install python origen 1 322 Jan-12-2024, 05:39 AM
Last Post: ndc85430
  How to display <IPython.core.display.HTML object>? pythopen 3 45,708 May-06-2023, 08:14 AM
Last Post: pramod08728
Information Unable to display joystick's value from Python onto display box MelfoyGray 2 2,173 Nov-11-2020, 02:23 AM
Last Post: MelfoyGray
  square root of 5 input numbers mrityunjoy 1 1,995 Jun-10-2020, 11:08 AM
Last Post: perfringo
  square root of 6 input numbers mrityunjoy 3 2,557 Jun-07-2020, 06:35 AM
Last Post: ndc85430
  How to simplify square finding program? meknowsnothing 3 2,837 Jun-11-2019, 08:20 PM
Last Post: meknowsnothing
  cropping a picture (always square) Leon 1 2,104 Aug-13-2018, 10:04 AM
Last Post: Leon
  Error when trying to square a number pistacheoowalnuttanker 5 3,767 Jul-20-2018, 02:23 AM
Last Post: pistacheoowalnuttanker
  Square and Cube roots. jarrod0987 2 5,343 Apr-13-2018, 09:30 PM
Last Post: casevh
  Understanding square bracket use wahsape 4 3,887 Jan-26-2018, 01:17 PM
Last Post: kismat77

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020