Python Output – print() Function in Python
Introduction
Python is a popular programming language known for its simple syntax and readability. One of the first concepts beginners learn when starting Python programming is how to display output.
Python provides a built-in function called print() that allows programmers to display text, numbers, and other values on the screen.
In this tutorial, you will learn how to use the Python print() function, print different types of values, display multiple values, and use the sep and end parameters with practical examples.
1. What is the print() Function in Python?
The print() function is a built-in Python function used to display output on the screen.
It can display strings, integers, floating-point numbers, Boolean values, and multiple values.
Syntax
print(value)
Here, value represents the information you want to display.
Example 1: Printing Hello World
print('Hello World')
Output:
Hello World
In this example, the print() function displays the text Hello World on the screen.
Example 2: Printing a String
print('salman khan')
Output:
salman khan
Strings must be enclosed within quotation marks, either single quotes (' ') or double quotes (" ").
Important: Python is a case-sensitive programming language. This means uppercase and lowercase letters are treated differently.
2. Printing Different Types of Values
The print() function can display different types of values, including integers, floating-point numbers, and Boolean values.
Printing an Integer
An integer is a whole number without a decimal point.
print(7)
Output:
7
Printing a Float
A float represents a number containing a decimal point.
print(7.7)
Output:
7.7
Printing a Boolean Value
Boolean values represent either True or False.
print(True)
Output:
True
You can also print False:
print(False)
Output:
False
3. Printing Multiple Values
Python allows you to display multiple values using a single print() statement.
By default, Python separates multiple values with a space.
Example
print('Hello', 1, 4.5, True)
Output:
Hello 1 4.5 True
In this example, the print() function displays four different values:
Hello– String1– Integer4.5– FloatTrue– Boolean
Python automatically separates these values with spaces.
4. Using the sep Parameter in Python
The sep parameter is used to specify the separator between multiple values passed to the print() function.
By default, Python uses a space as the separator.
Example 1: Using a Slash Separator
print('Hello', 1, 4.5, True, sep='/')
Output:
Hello/1/4.5/True
Example 2: Using a Hyphen Separator
print('Python', 'Programming', 'Language', sep='-')
Output:
Python-Programming-Language
Example 3: Using a Comma Separator
print('Apple', 'Banana', 'Mango', sep=',')
Output:
Apple,Banana,Mango
The sep parameter is useful when you want to format multiple values with a specific separator.
5. Using the end Parameter in Python
By default, the print() function moves the cursor to a new line after displaying output.
The end parameter allows you to change what is printed at the end of a print() statement.
Example 1: Default Behaviour
print('hello')
print('world')
Output:
hello
world
Each print() statement displays its output on a separate line.
Example 2: Using the end Parameter
print('hello', end='-')
print('world')
Output:
hello-world
In this example, end='-' replaces the default newline character with a hyphen.
Example 3: Using an Empty String
print('Python', end='')
print('Programming')
Output:
PythonProgramming
Because the end parameter is an empty string, Python does not add a space or newline between the two outputs.
6. Difference Between sep and end Parameters
Both sep and end are parameters of the Python print() function, but they serve different purposes.
| Parameter | Purpose | Default Value |
|---|---|---|
sep | Separates multiple values | Space |
end | Specifies what appears after the output | Newline (\n) |
Example
print('Hello', 'Python', sep='-', end='!')
print('World')
Output:
Hello-Python!World
Here:
sep='-'places a hyphen between the two values.end='!'adds an exclamation mark instead of a newline.