How can I use 2 digits format for all the number?
How can I use 2 digits format for all the number?
|
Aug-09-2021, 07:58 AM
In what? Console program? Spreadsheet? Web page?
Aug-09-2021, 08:25 AM
Aug-09-2021, 08:42 AM
number = 2 print(f'number is {number:02}')
Aug-09-2021, 08:48 AM
(Aug-09-2021, 08:42 AM)Yoriz Wrote:Thanks.number = 2 print(f'number is {number:02}') 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. (Aug-09-2021, 08:48 AM)plumberpy Wrote: [[1 2 3],[11 22 33],[1 32 6]] to be printed aslist 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))) 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"))
|
|
Users browsing this thread: 1 Guest(s)