Printing Text and Blank Lines
print('This is a heading') print() print('This is another heading')
Printing Variables (for debugging)
# print(dice) #
Printing Text and Variables (requires Python 3.6)
There must be an “f” before the first apostrophe, otherwise what is inside the curly brackets will be printed exactly.
This is called an “f-string” (formatted-string)
# print(f'The capital of {country} is {capital}.') #
Printing in Columns (requires Python 3.6)
Each variable can be given a width, extra spaces will be added if needed.
# print(f'The capital of {country:15} is {capital:15}.') #
Printing Decimals (requires Python 3.6)
Floating-point numbers can be printed out to a specified number of decimal places.
This is not the same as rounding the numbers, which is permanent.
# print(f'Price = £{price:.2f}') #
Printing in Colour
This not required for the course, but does can easily make your programs more user friendly.
Notes:
- The autoreset=True in the init() command will reset colours and styles after every print or input. If this option is left out, then colours and styles remain active until they are changed or reset (Fore.RESET and Style.RESET_ALL).
- Be consistent with your colours – have one colour for input commands, another colour for errors, another colour for messages, etc.
- Thonny must be at least version 3.3.10 – the typed colour/style for answers to input commands is fixed by the theme that you are using.
from colorama import init, Fore, Style init(autoreset=True) name = input(f"{Fore.BLACK}Enter your name:") print(f"{Fore.BLUE}Hello {Style.BRIGHT}{name}{Style.NORMAL}")