The tab is working properly, you probably are looking for .ljust(width)
lems = ['scaena', 'persona', 'improbus']
for i in lems:
print('{}{}'.format(i.ljust(10), 'whatever'))
prints out:
scaena whatever
persona whatever
improbus whatever
Sometimes it's useful to calculate the max length of your strings so the output is more reliable
lems = ['scaena', 'persona', 'improbus']
max_len = max(len(l) for l in lems)
for i in lems:
print('{}{}'.format(i.ljust(max_len + 1), 'whatever'))
Answer from Gonzo on Stack OverflowThe tab is working properly, you probably are looking for .ljust(width)
lems = ['scaena', 'persona', 'improbus']
for i in lems:
print('{}{}'.format(i.ljust(10), 'whatever'))
prints out:
scaena whatever
persona whatever
improbus whatever
Sometimes it's useful to calculate the max length of your strings so the output is more reliable
lems = ['scaena', 'persona', 'improbus']
max_len = max(len(l) for l in lems)
for i in lems:
print('{}{}'.format(i.ljust(max_len + 1), 'whatever'))
I have used that construction:
tab = '\t'
file.write(f'''Name{tab}sn{tab}port{tab}sw_sn{tab}sw_port''')
This might be what you want.
>>> test_str = '''
... list {}
... list
... {}
... '''
>>> values
['1', '2', '3']
>>> print(test_str.format(','.join(values), ',\n\t'.join(values)))
list 1,2,3
list
1,
2,
3
Addendum:
Notice that the function creates the string that it returns in the form of a new-fangled f string, thus allowing the possibility of substitution of the values returned by functions or other expressions within it.
>>> def a_string(v1, v2):
... def f1(val):
... return '---'+val
... def f2(val):
... return '+++'+val
... return f'''{f1(v1)} is bigger than {f2(v2)}'''
...
>>> a_string('2', '1')
'---2 is bigger than +++1'
You could use a function to make it possible to use only one set of values as follow
test_str="""
list {values1}
list
\t{values2}
"""
def test_format(values):
values1 = ",".join(values)
values2 = ",\n\t".join(values)
return test_str.format(values1=values1, values2=values2)
print test_format(["1", "2", "3"])
how do i force every line of a multiline replacement string variable to maintain the same indentation level as the variable to which it is assigned inside a large multline f-string
or in other words if i have a large f-string and inside it i have {}'s which are themselves attached to smaller multiline f-strings which are dynamically created how do i, in the larger string, force every line in each smaller string to maintain the same indentation level within the large string as the variable it is assigned to. not counting \t characters in the smaller f-strings
related question how do i unindent an f-string aka what is the character equivalent, if there is one, of shift-tab