0
0
Pythonprogramming~5 mins

print() function basics in Python

Choose your learning style9 modes available
Introduction

The print() function shows messages or results on the screen. It helps you see what your program is doing.

You want to show a greeting message to the user.
You want to check the value of a variable while writing code.
You want to display the result of a calculation.
You want to print multiple pieces of information in one line.
You want to add blank lines or spaces for better readability.
Syntax
Python
print(value1, value2, ..., sep=' ', end='\n')

You can print one or more values separated by commas.

sep sets what goes between values (default is space). end sets what prints at the end (default is new line).

Examples
Prints a simple message.
Python
print('Hello, world!')
Prints two words separated by a space.
Python
print('Hello', 'there')
Prints words separated by a dash instead of space.
Python
print('Hello', 'there', sep='-')
Prints without a new line after first print, so second print continues on same line.
Python
print('Hello', end='!')
print(' How are you?')
Sample Program

This program prints a name and age, then a welcome message with dashes, then a goodbye message followed by a blank line, and finally a see you soon message.

Python
name = 'Alice'
age = 30
print('Name:', name)
print('Age:', age)
print('Welcome', name, 'to the program!', sep=' - ')
print('Goodbye!', end='\n\n')
print('See you soon!')
OutputSuccess
Important Notes

Use commas to print multiple items easily without converting to strings.

Remember print() adds a new line by default after printing.

You can change the end character to keep printing on the same line.

Summary

print() shows messages or values on the screen.

You can print many things at once separated by commas.

Use sep and end to control spacing and line breaks.