fuzunspm fuzunspm - 1 year ago 30
Python Question

How to align results left and right at the same time

Using PyQt4, pyodbc and python 3.5.1 i want to align result rows left and right at the same time. i mean:

printing resultrow = "column1:column2"

but i want to print it like

printing resultrow = "column1 : column2"

i'm writing result data to Qt4 App Plain Text Edit Field. Because of results are changing i don't know how many spaces i need to add so i need to align them on Plain Text Edit Field but it's only for one way, at least as far as i know. Please advice me to fix this issue.

Thank you...


You can do alignment and space padding with the normal python string formatting

>>> '{:<15}:{:>15}'.format('column1', 'column2')
'column1        :        column2'

If you want to make the padding variable

width = len(row)
fmt_str = '{{:<{0}}}:{{:>{0}}}'.format(width)
fmt_str.format('column1', 'column2')