Search code examples
pythonpython-3.xloopsprintingstring-formatting

How to insert extra element before an iteration without affecting the distance between existing printed elements


Input:

a = ['x', 'y', 'z']

Syntax:

for i in a:
    print(f'{i:<14}', end = '')

Current output:

x             y             z

Expected output

Title                                   x             y             z   

Solution

  • Print "Title" before the loop:

    print("Title", end=' ' * 25)
    for i in a:
        print(f'{i:<14}', end = '')
    

    Output:

    Title                         x             y             z