You can use enumerate and string formatting for this:
num_list = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]
for _index, _value in enumerate(num_list):
if val <= 0:
_value = ""
elif val >= 100
_value = 100
num_list[_index] = "{:>4}".format(_value)
>> num_list
>> [' 1.0', ' 2.0', ' 3.0', ' 4.0', ' 5.0', ' 6.0']
The format "{:<4}"determines the formatting of the text, <means alignment of the text on the left, and >means alignment on the right, and ^means its centering. 4defines the final length. It applies the preferred alignment and fills the rest with spaces. If you want to use the fill character instead of spaces, you can use it as
num_list[_index] = "{:*<4}".format(_value)
>> num_list
>> ['1.0*', '2.0*', '3.0*', '4.0*', '5.0*', '6.0*']
Where *is your fill character
source
share