0
0
Pythonprogramming~5 mins

print() parameters and formatting in Python - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the purpose of the sep parameter in the print() function?
The sep parameter defines the string inserted between multiple values printed. By default, it is a space (' ').
Click to reveal answer
beginner
How does the end parameter affect the print() output?
The end parameter sets what is printed at the end of the output. By default, it is a newline (\n), so the next print starts on a new line.
Click to reveal answer
intermediate
What does the file parameter do in print()?
The file parameter specifies where the output goes. By default, it prints to the screen (standard output), but you can redirect it to a file or other stream.
Click to reveal answer
beginner
How can you format a number to show two decimal places using print()?
Use an f-string with format specifier like f"{number:.2f}". This rounds and shows the number with two decimals.
Click to reveal answer
beginner
What is the difference between print('Hello', end='!') and print('Hello')?
The first prints Hello! without moving to a new line after. The second prints Hello and moves to the next line.
Click to reveal answer
What is the default separator between values in print()?
AA space
BA comma
CNo separator
DA newline
Which print() parameter changes what is printed at the end?
Asep
Bflush
Cfile
Dend
How do you print multiple values without spaces between them?
AUse <code>end=''</code>
BUse <code>sep=' '</code>
CUse <code>sep=''</code>
DUse <code>file=''</code>
What does print(f"{value:.2f}") do?
APrints value as integer
BPrints value with 2 decimal places
CPrints value in scientific notation
DPrints value as string
How can you print output to a file instead of the screen?
AUse <code>print(..., file=open('file.txt', 'w'))</code>
BUse <code>print(..., file=filename)</code> where filename is a string
CUse <code>print(..., sep='file.txt')</code>
DUse <code>print(..., end='file.txt')</code>
Explain how the sep and end parameters change the behavior of the print() function.
Think about what appears between and after printed items.
You got /4 concepts.
    Describe how to format a floating-point number to two decimal places when printing in Python.
    Use Python's modern string formatting.
    You got /4 concepts.