Changes

Jump to: navigation, search

OPS435 Python Lab 4

3 bytes removed, 10:40, 23 June 2017
PART 3 - String Formatting Advanced Features
print('{0:10}{1}'.format(string1, string2)) # Make sure string1 is 10 characters aligned to the left
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 do this is '''<''' which 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 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
13,420
edits

Navigation menu