13,420
edits
Changes
→PART 3 - String Formatting Advanced Features
# With positional argument numbers
print('{0:^10} {1:^10} {2:^10}\n{3:^10} {4:^10} {5:^10}'.format('abc', 'def', 'ghi', 'jkl123', 'mno123', 'pqr123'))
</source>:#The alignment character can be changed to any other character that is wanted for the output. By default it's a space, but it could be anything else by adding an additional character '''{:M<10}''' such as '''M''' or '''*''' before the alignment character <br><br>:#To see 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('{:.^10} {:.^10} {:.^10}\n{:.^10} {:.^10} {:.^10}'.format('abc', 'def', 'ghi', 'jkl123', 'mno123', 'pqr123')) # Without positional argument numbers
</source>:#Make You can make the output look appear better by creating borders, . Below is a function in order to create this function that displays some a border containing data enclosed in . If you learned MySQL, you may have seen bordersused to display table data, try etc. Try defining and running the user-defined function below to understand see what is happeninghappens. This function does not return any value, only prints formatted text<br><br>:#Issue the following:<source># Define the function "print_with_borders()":
def print_with_borders():
print('|{:-^10}|'.format('nums'))
print('|{}|'.format('-'*10))
# Run the function"print_with_borders()":
print_with_borders()
</source>