Python print in two columns

I'm trying to print a string with two fixed columns. For example, I'd like to be able to print:

abc       xyz
abcde     xyz
a         xyz

What is the correct way to format the output string when printing to achieve this? Also, how is this done before version 2.6 and after version 2.6?

Python print in two columns

martineau

115k25 gold badges160 silver badges283 bronze badges

asked Feb 6, 2016 at 2:46

Python print in two columns

Victor BrunellVictor Brunell

4,87810 gold badges28 silver badges44 bronze badges

You can use format and mention fix spaces between columns

'{0:10}  {1}'.format(s1, s2)

Old Style formatting

'%-10s' '%s' % (s1,s2)

answered Feb 6, 2016 at 2:59

Python print in two columns

5

This should work for all lengths of the elements (assuming they are strings. This assumes your data is in two seperate lists first and second.

maxlen = len(max(first, key=len))

for i,j in zip(first, second):
    print "%s\t%s" % (i.ljust(maxlen, " "), j)

This works in Python 2.x, before and after 2.6.

answered Feb 6, 2016 at 2:59

Python print in two columns

L3viathanL3viathan

25.8k2 gold badges55 silver badges74 bronze badges

2

Prior to >=python3.6

s1='albha'
s2='beta'

f'{s1}{s2:>10}'

#output
'albha      beta'

answered Feb 14, 2018 at 22:38

RoushanRoushan

3,6583 gold badges20 silver badges37 bronze badges

There are a number of ways of doing this and it depends on how the data is stored. Assumining your data is stored in equal length lists:

for i in range(len(list1)):
    print(“%3i\t%3i” %(list1[i],list2[i]))

This will work in all versions of python. The 3i ensures the output has a field width of 3 characters

answered Feb 6, 2016 at 2:51

BenJBenJ

4562 silver badges7 bronze badges

6

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier.

    Let’s discuss all different ways of selecting multiple columns in a pandas DataFrame.

    Method #1: Basic Method

    Given a dictionary which contains Employee entity as keys and list of those entity as values.

    import pandas as pd

    data = {'Name':['Jai', 'Princi', 'Gaurav', 'Anuj'],

            'Age':[27, 24, 22, 32],

            'Address':['Delhi', 'Kanpur', 'Allahabad', 'Kannauj'],

            'Qualification':['Msc', 'MA', 'MCA', 'Phd']}

    df = pd.DataFrame(data)

    df[['Name', 'Qualification']]

    Output:

    Python print in two columns

    Select Second to fourth column.

    import pandas as pd

    data = {'Name':['Jai', 'Princi', 'Gaurav', 'Anuj'],

            'Age':[27, 24, 22, 32],

            'Address':['Delhi', 'Kanpur', 'Allahabad', 'Kannauj'],

            'Qualification':['Msc', 'MA', 'MCA', 'Phd']}

    df = pd.DataFrame(data)

    df[df.columns[1:4]]

    Output:

    Python print in two columns

    Method #2: Using loc[]

    Example 1: Select two columns

    import pandas as pd

    data = {'Name':['Jai', 'Princi', 'Gaurav', 'Anuj'],

            'Age':[27, 24, 22, 32],

            'Address':['Delhi', 'Kanpur', 'Allahabad', 'Kannauj'],

            'Qualification':['Msc', 'MA', 'MCA', 'Phd']}

    df = pd.DataFrame(data)

    df.loc[1:3, ['Name', 'Qualification']]

    Output:

    Python print in two columns

    Example 2: Select one to another columns. In our case we select column name “Name” to “Address”.

    import pandas as pd

    data = {'Name':['Jai', 'Princi', 'Gaurav', 'Anuj'],

            'Age':[27, 24, 22, 32],

            'Address':['Delhi', 'Kanpur', 'Allahabad', 'Kannauj'],

            'Qualification':['Msc', 'MA', 'MCA', 'Phd']}

    df = pd.DataFrame(data)

    df.loc[0:1, 'Name':'Address']

    Output:

    Python print in two columns

    Example 3: First filtering rows and selecting columns by label format and then Select all columns.

    import pandas as pd

    data = {'Name':['Jai', 'Princi', 'Gaurav', 'Anuj'],

            'Age':[27, 24, 22, 32],

            'Address':['Delhi', 'Kanpur', 'Allahabad', 'Kannauj'],

            'Qualification':['Msc', 'MA', 'MCA', 'Phd']

           }

    df = pd.DataFrame(data)

    df.loc[0, :]

    Output:

    Python print in two columns

    Method #3: Using iloc[]

    Example 1: Select first two column.

    import pandas as pd

    data = {'Name':['Jai', 'Princi', 'Gaurav', 'Anuj'],

            'Age':[27, 24, 22, 32],

            'Address':['Delhi', 'Kanpur', 'Allahabad', 'Kannauj'],

            'Qualification':['Msc', 'MA', 'MCA', 'Phd']}

    df = pd.DataFrame(data)

    df.iloc[:, 0:2

    Output:

    Python print in two columns

    Example 2: Select all or some columns, one to another using .iloc.

    import pandas as pd

    data = {'Name':['Jai', 'Princi', 'Gaurav', 'Anuj'],

            'Age':[27, 24, 22, 32],

            'Address':['Delhi', 'Kanpur', 'Allahabad', 'Kannauj'],

            'Qualification':['Msc', 'MA', 'MCA', 'Phd']}

    df = pd.DataFrame(data)

    df.iloc [0:2, 1:3]

    Output:

    Python print in two columns

    Method #4: Using .ix

    Select all or some columns, one to another using .ix.

    import pandas as pd

    data = {'Name':['Jai', 'Princi', 'Gaurav', 'Anuj'],

            'Age':[27, 24, 22, 32],

            'Address':['Delhi', 'Kanpur', 'Allahabad', 'Kannauj'],

            'Qualification':['Msc', 'MA', 'MCA', 'Phd']}

    df = pd.DataFrame(data)

    print(df.ix[:, 0:2])

    Output:

    Python print in two columns


    How do I print two columns?

    Assuming you have a list of data in one column that you want to print in multiple columns:.
    Select the cells you want to print..
    Click File, and then click Print. The Print dialog box appears..
    In the Settings area, click Columns, and then click the number of columns you want..
    Click OK..

    How do I print two columns side by side in Python?

    You can use the zip() function to join lists together. The zip() function will iterate tuples with the corresponding elements from each of the lists, which you can then format as Michael Butscher suggested in the comments. Finally, just join() them together with newlines and you have the string you want.

    How do you print in columns in Python?

    Print With Column Alignment in Python.
    Use the % Formatting to Print With Column Alignment in Python..
    Use the format() Function to Print With Column Alignment in Python..
    Use f-strings to Print With Column Alignment in Python..
    Use the expandtabs() Function to Print With Column Alignment in Python..

    How do I print text side by side in Python?

    Print two outputs side-by-side, in Python side_by_side allows users to print two multi-line outputs side-by-side. This produces an effect similar to running diff -y file1 file2 in a Unix system.