Python Forum
How to left align the columns
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to left align the columns
#1
Hi,

I have the below code, and I store data in str. Now I want to print this str using left align. I use below code but it is not left-aligned.

my_list= ['1','4','6']
result = ''
for i in range(len(my_list)):
    if int(my_list[i]) < 3:
        print(my_list[i])
        result = result +'\n'+ "[INFO] " +"Failed case"+ "Fail" +' 12'
    elif int(my_list[i]) >=4 and int(my_list[i]) <6:
        result = result +'\n'+ "[INFO] " + "Success case" + "Pass" +'  122'
    else:
        result = result +'\n'+ "[INFO] " + "unsuccess case" + "Pass" +' 0'
        
print(result)
current output:

[INFO] Failed caseFail 12
[INFO] Success casePass   122
[INFO] unsuccess casePass 0
Desired output:

[INFO] Failed caseFail    12
[INFO] Success casePass   122
[INFO] unsuccess casePass 0
Reply
#2
Use string formatting,now should use f-string.
It look a lot nicer without all those +.
{12:^8} center alignment this is stuff that only work in string formatting.
A couple of other changes,like using enumerate and convert to list integers first.
my_list= ['1','4','6']
my_list = [int(i) for i in my_list]
result = ''
for i,item in enumerate(my_list):
    if my_list[i] < 3:
        result = result + f'\n[INFO] Failed case Fail {12:^8}'
    elif my_list[i] >= 4 and my_list[i] < 6:
        result = result + f'\n[INFO] Success case Pass {122:^8}'
    else:
        result = result + f'\n[INFO] unsuccess case Pass 0'

print(result)
Output:
[INFO] Failed case Fail 12 [INFO] Success case Pass 122 [INFO] unsuccess case Pass 0
Reply
#3
but why we only use:^8 for two cases, and did not use "unsuccess case Pass 0". If it is a big string, then we can not check with an eyeball. any general form?
Reply
#4
This is very static case as it's just strings with different size,and no insert of variables from the loop.
Could probably had rewrite code a little if look more into it.

Here some example with < ^ > alignment in a loop.
for i in range(3,13):
    print(f"{i:<6} {i*i:<6} {i*i*i:<6}")
print('-'*20)
for i in range(3,13):
    print(f"{i:^6} {i*i:^6} {i*i*i:^6}")
print('-'*20)
for i in range(3,13):
    print(f"{i:>6} {i*i:10} {i*i*i:<6}")
Output:
3 9 27 4 16 64 5 25 125 6 36 216 7 49 343 8 64 512 9 81 729 10 100 1000 11 121 1331 12 144 1728 -------------------- 3 9 27 4 16 64 5 25 125 6 36 216 7 49 343 8 64 512 9 81 729 10 100 1000 11 121 1331 12 144 1728 -------------------- 3 9 27 4 16 64 5 25 125 6 36 216 7 49 343 8 64 512 9 81 729 10 100 1000 11 121 1331 12 144 1728
Reply
#5
#%%
max_width = 15
a = ''
for i in range(3,8):
    if i <5:
        print(f"{'Pass':<15} {'Par':<15} {'Failcase':<15}")
        a =a+'-1'+","
    else:
        print(f"{'PassFail':<15} {'Par':<15} {'FailFP':<15}")
        #print('-'*20)
        a =a+'1'+","
I am appending 1, or -1 on each iteration to 'a'. But, I don't want to print coma at the end. Second, instead of define 15, can I define max_width, and replace 15 with max_width. as below. I tried but I am getting error.ValueError: Invalid format specifier .

#%%
max_width = 15
a = ''
for i in range(3,8):
    if i <5:
        print(f"{'Pass':<15} {'Par':<max_widht} {'Failcase':<max_widrh}")
        a =a+'-1'+","
    else:
        print(f"{'PassFail':<max_width} {'Par':<max_width} {'FailFP':<max_width}")
        #print('-'*20)
        a =a+'1'+","
Reply
#6
As value max_width is coming in from outside,most use {max_width}.
max_width = 15
a = ''
for i in range(3,8):
    if i < 5:
        print(f"{'Pass':<{max_width}} {'Par':<{max_width}} {'Failcase':<{max_width}}")
        a = a + '-1' + ","
print(a.strip(','))
Output:
Pass Par Failcase Pass Par Failcase -1,-1
Reply
#7
I have string 0,0,-1
from below output:

but when I printa.count('1') it printing 1 but I don't want to consider -1, how to count only '1' not excluding '-1'.
#%%
max_width = 15
a = ''
for i in range(3,6):
    if i < 2:
        print(f"{'Pass':<15} {'Par':<15} {'Failcase':<15}")
        a =a + '1' +","
    elif i >= 5 and i <= 7:
        print(f"{'PassFail':<15} {'Par':<15} {'FailFP':<15}")
        #print('-'*20)
        a = a + '-1' + ","
    else:
        print(f"{'PassFail':<15} {'Par':<15} {'FailFP':<15}")
        #print('-'*20)
        a = a + '0'+ ","
    print(a)

if a.count('-1') > 0:
    print('-1')
elif a.count('1') > 0:
    print('1')
else:
    print('0')
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to auto align x-axis label SamLiu 2 878 Jan-27-2023, 11:10 PM
Last Post: SamLiu
  PyQT5 - align left frohr 7 3,971 May-07-2022, 09:56 PM
Last Post: deanhystad
  How did one column get left-justified? Mark17 6 1,946 Feb-26-2022, 11:55 PM
Last Post: deanhystad
  Explanation of the left side of this statement please rascalsailor 3 2,506 Sep-09-2020, 02:02 PM
Last Post: rascalsailor
  Center align Kristenl2784 1 1,959 Aug-03-2020, 04:25 PM
Last Post: bowlofred
  How to left align logging messages Mekala 3 6,852 Jun-28-2020, 04:04 PM
Last Post: bowlofred
  Why is left mouse click not working? yeto 3 6,192 Jul-15-2019, 05:23 AM
Last Post: Yoriz
  str.format rounding to the left of the decimal ClassicalSoul 2 2,486 Mar-27-2019, 11:12 AM
Last Post: perfringo
  Move a character left or right in a file. DreamingInsanity 4 4,867 Mar-21-2019, 07:52 PM
Last Post: DreamingInsanity

Forum Jump:

User Panel Messages

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