5

I am trying to form a string using format() but can't figure this out.

lems = ['scaena', 'persona', 'improbus']
for i in lems:
    print('{}{}{}'.format(i, '\t', 'whatever'))

but the tab is not strung as I expect it. Actually, the last item in the list the tab does apply as it prints out:

scaena  text
persona text
improbus        text

What is going on? thanks.

3
  • 2
    It is applying properly in all of them. Look. Commented Mar 1, 2019 at 21:46
  • Ok, if I do repr(i) I do see the tabs, however what's up with the extra spaces and why is it that is i write this to a text file my gedit text editor editor does not recognize the tab? Commented Mar 1, 2019 at 22:26
  • The extra spaces are how it gets to the next tab stop... Commented Mar 2, 2019 at 5:57

5 Answers 5

12

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'))
Sign up to request clarification or add additional context in comments.

1 Comment

I don't understand how it is working properly when I can count different spaces between the words, 4 between scaena and whatever, 1 between persona and whatever, the third line has 8 spaces. Also, I NEED the tab because I later write this to a txt file and and the text editor does not read those as tabs.
5

I have used that construction:

tab = '\t'
file.write(f'''Name{tab}sn{tab}port{tab}sw_sn{tab}sw_port''')

1 Comment

Just using \t directly in the f-string works as well.
2

f-string is another option to achieve the same thing as the answer but in a more compact format. '<10' means to right align with width 10.

lems = ['scaena', 'persona', 'improbus']
for i in lems:
    print(f"{i:<10} whatever")

Output:

scaena     whatever
persona    whatever
improbus   whatever

Comments

1

You can also use the f-string:

lems = ['scaena', 'persona', 'improbus']
for i in lems:
    print(f"{i}\t{'whatever'}")

You can use the function repr() to see that tab is added to the string:

from reprlib import repr

lems = ['scaena', 'persona', 'improbus']
for i in lems:
    print(repr(f"{i}\t{'whatever'}"))

Output:

'scaena\twhatever'
'persona\twhatever'
'improbus\twhatever'

1 Comment

one up for the f"{i}\t{'whatever'}
0

The issue I was having was python unrelated so my question has already the answer in it which I reiterate and mark as the correct answer:

for i in lems:
    i = i.strip()
    print('{}{}{}'.format(i, '\t', 'text'))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.