Python Console Output

Display text (and spaces/numbers/symbols):

print("Hello World 123 @$%#.,")

Arrange text (tab, new line):

print("Hello \t World \n Testing")

- "\t" will tab "world" to the right

- "\n" will put the text following it onto a new line

Display calculated values:

print(12+34) # This will display: 46

- With calculations you don't use quotes, otherwise it will display the calculation numbers "12+34" rather than the answer

Display a value with 2 decimal places:

print("It weighs %.2f Kg" % 14.234092) # This will display "It weighs 14.23 Kg"

- You can display to another number of decimals places, just change the number after %.

Have content take up a fixed amount of space

quantity = 92 print(f"{quantity:<20}apples") # Alternatively print("{:<20}".format(quantity) + "apples")

- This will add padding after the number so that it takes up 20 spaces total

- This is useful if you have another value on the same line and want it to be a fixed distance from the left side

- Use this if you have multiple lines containing words/numbers of varying length that you want to line up in columns

Display content aligned to the right

quantity = 92 print(f"{quantity:>12}") # Alternatively print("{:>12}".format(quantity))

- This will add padding before the number so that it takes up 12 spaces total

Challenge

Write a program that will display the following on a line (where the blank line should show the actual answer calculated by the program, <tab> is replaced by a real position tab, and the numbers are shown to the 3rd decimal):

<right aligned> name = Bob

<tab>345 divided by 78 equals ___.

Completed