Python Forum
How can I use 2 digits format for all the number?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can I use 2 digits format for all the number?
#1
Photo 
How can I fix all the numbers to 2 digits format so that the numbers will be all aligned column wise in the rows below?

Thanks.
   
Reply
#2
In what? Console program? Spreadsheet? Web page?
Reply
#3
(Aug-09-2021, 07:58 AM)deanhystad Wrote: In what? Console program? Spreadsheet? Web page?
Sorry, in Python.
Reply
#4
number = 2
print(f'number is {number:02}')
Output:
number is 02
Reply
#5
(Aug-09-2021, 08:42 AM)Yoriz Wrote:
number = 2
print(f'number is {number:02}')
Output:
number is 02
Thanks.

My apology for not being clear.

I meant from printing the numbers in lists within a list (sorry I do not know how to express that). Eg
[[1 2 3],[11 22 33],[1 32 6]] to be printed as

[[ 1 2 3], [ 11 22 33], [ 1 32 6]]

As format 3 digits (not 2 digits, realised I need a space in between the numbers).


Thanks.
Reply
#6
(Aug-09-2021, 08:48 AM)plumberpy Wrote: [[1 2 3],[11 22 33],[1 32 6]] to be printed as

[[ 1 2 3], [ 11 22 33], [ 1 32 6]]
list is a internal structure,when want display in output is common to take values of list and format it(without []).
lst = [[1, 2, 3], [11, 22, 33], [1, 32, 6]]
for inner in lst:
    print(' | '.join((f"{word:^2}" for word in inner)))
Output:
1 | 2 | 3 11 | 22 | 33 1 | 32 | 6
Tabulate can easily give fancier output.
from tabulate import tabulate

lst = [[1, 2, 3], [11, 22, 33], [1, 32, 6]]
print(tabulate(lst))
print(tabulate(lst, tablefmt="fancy_grid"))
print(tabulate(lst, tablefmt="github"))
Output:
-- -- -- 1 2 3 11 22 33 1 32 6 -- -- -- ╒════╤════╤════╕ │ 1 │ 2 │ 3 │ ├────┼────┼────┤ │ 11 │ 22 │ 33 │ ├────┼────┼────┤ │ 1 │ 32 │ 6 │ ╘════╧════╧════╛ |----|----|----| | 1 | 2 | 3 | | 11 | 22 | 33 | | 1 | 32 | 6 |
Reply
#7
Beautiful!

I was also looking for the grids.

Many many thanks!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Digits of a number 1234 2 1,840 Nov-27-2020, 05:43 PM
Last Post: perfringo
  Single digits seem to be greater than double digits Frosty_GM 4 3,508 Nov-20-2020, 10:13 AM
Last Post: DeaD_EyE
  Please support regex for version number (digits and dots) from a string Tecuma 4 3,191 Aug-17-2020, 09:59 AM
Last Post: Tecuma
  multiple number format conversion oli_action 4 2,582 Aug-11-2020, 05:10 AM
Last Post: perfringo
  Number format william888 3 2,838 Aug-23-2019, 05:33 AM
Last Post: ThomasL
  Changing Number Format moby 4 5,055 May-24-2019, 11:04 PM
Last Post: snippsat
  Counting the number of files related to particular format ambush 3 2,935 Nov-05-2018, 08:58 AM
Last Post: buran
  summing digits of a number Megabeaz 3 2,656 Jun-29-2018, 02:55 PM
Last Post: ichabod801
  I'm dividing large numbers of about 25 million digits I need to retain decimal format Pleiades 2 3,182 Apr-26-2018, 07:50 AM
Last Post: Pleiades
  Input a number with any number of digits and output it in reverse order of digits. sarada2099 6 6,663 Mar-12-2017, 05:45 PM
Last Post: Ofnuts

Forum Jump:

User Panel Messages

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