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
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
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?
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
#%%
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'+","
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
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')