13,420
edits
Changes
→PART 3 - String Formatting Advanced Features
print('{0:6}{1}'.format(string1, string2)) # Make sure string1 is 6 characters aligned to the left
</source>
:#By default , the format() function aligns to the left, the symbol to explicitely do this is '''<''' which looks like is a shortcut for : '''{0:<10}''' when used in the curely braces. Now whenever a different string is placed inside it always aligns to the left 10 characters. This allows for INCREDIBLY concise code to generate well structured output<br><br>:#To demonstrate this, issue the following:<source>
print('{:<10} {:<10} {:<10}\n{:<10} {:<10} {:<10}'.format('abc', 'def', 'ghi', 'jkl123', 'mno123', 'pqr123')) # Without positional argument numbers
print('{0:<10} {1:<10} {2:<10}\n{3:<10} {4:<10} {5:<10}'.format('abc', 'def', 'ghi', 'jkl123', 'mno123', 'pqr123')) # With positional argument numbers