-2
    Ackme     =   125.000 
    T-radium  =   124.000
    Average   =    87.621 
    Maximum   =    77.000
    Minimum   =    16.120
    Variance  =   311.271
    Std&Dev   =    112518
    Median    =    582000
    Validcos  =     1.000
    T-range   =     7.000
    Maximum   =     9.500
    Minimum   =     9.000
    Variance  =     0.000
    Std&Dev   =     0.020
    Median    =     8.210

How am I suppose to use the format and f-string to have such a print result? All the variables like validnum and Average are output from the same program so the variable name is fixed but the output number could be varied with different number input. I tried to use print('Count = ', '{:{width}.{prec}f}') but it doesn't work. This is a python code. It can't make all the "=" align in this case.

  • 1
    Please show a [mre] of what isn't working. Otherwise this is covered in https://stackoverflow.com/questions/32808383/formatting-numbers-so-they-align-on-decimal-point – mkrieger1 Oct 08 '21 at 10:10

1 Answers1

0

This version uses the str.format method.

str.format(*args, **kwargs)

Perform a string formatting operation. The string on which this method is called can contain literal text or replacement fields delimited by braces {}. Each replacement field contains either the numeric index of a positional argument, or the name of a keyword argument. Returns a copy of the string where each replacement field is replaced with the string value of the corresponding argument.

count = 125.000
ValidNum = 124.000
Average = 57.621
Variance = 306.871
Dev = 17.518

print('Count = ', '{:<8{width}.{prec}f}'.format(count,width=7,prec=3))
print('Count = ', '{:<8{width}.{prec}f}'.format(ValidNum,width=7,prec=3))
print('Count = ', '{:<8{width}.{prec}f}'.format(Average,width=7,prec=3))
print('Count = ', '{:<8{width}.{prec}f}'.format(Variance,width=7,prec=3))
print('Count = ', '{:<8{width}.{prec}f}'.format(Dev,width=7,prec=3))

Output:

Count =  125.000                                                                                
Count =  124.000                                                                                
Count =  57.621                                                                                 
Count =  306.871                                                                                
Count =  17.518     
ncica
  • 7,015
  • 1
  • 15
  • 37
  • Thanks for the help, but if you change the count to smthing else it won't work then.. – Andrew Swainskeren Oct 08 '21 at 11:16
  • 1
    @AndrewSwainskeren then it is not entirely clear what your input is and what the expected output is. you have to be more specific – ncica Oct 08 '21 at 11:56
  • Yeah, I'm sorry. But the numbers should be aligned from the right side. And all the words(the left side of the ''=") should be aligned from the left side. Also I think the idth should be 10 rather than 8 maybe? – Andrew Swainskeren Oct 08 '21 at 12:20