0
0
Pythonprogramming~5 mins

print() parameters and formatting in Python

Choose your learning style9 modes available
Introduction

The print() function shows information on the screen. You can control how it looks and what it shows by using its parameters.

You want to show messages or results to the user.
You need to print multiple items with spaces or other separators.
You want to print without moving to a new line each time.
You want to format numbers or text neatly before showing.
You want to add special characters like tabs or new lines in the output.
Syntax
Python
print(*objects, sep=' ', end='\n', file=None, flush=False)

*objects means you can print many things separated by commas.

sep sets what goes between items (default is space).

end sets what prints at the end (default is new line).

Examples
Prints two words separated by a space, then moves to a new line.
Python
print('Hello', 'World')
Prints words separated by a dash instead of space.
Python
print('Hello', 'World', sep='-')
Prints 'Hello!' and then 'World' on the same line.
Python
print('Hello', end='!')
print('World')
Prints a number formatted to 2 decimal places using f-string.
Python
print(f'Number: {3.14159:.2f}')
Sample Program

This program shows how to use sep to separate items with commas, end to avoid new line, and f-string formatting to round a number.

Python
print('Apples', 'Oranges', 'Bananas', sep=', ')
print('Loading', end='...')
print('Done!')
value = 7.12345
print(f'Value rounded: {value:.2f}')
OutputSuccess
Important Notes

You can print many items by separating them with commas inside print().

Use sep to change the space between items, like commas or dashes.

Use end to control what happens after printing, like staying on the same line.

Summary

print() shows information on the screen.

You can change spacing with sep and line endings with end.

Use f-strings to format numbers or text inside print().