The print method in Python

Subscribe to my newsletter and never miss my upcoming articles

In this post, we will explore the parameters of the print() function. In Python, the print() function is used to print something on the console.

Syntax

print("whatever you want to print..")

Example

Consider the below program,

a = [1,2,3,4]
for i in a:
  print(i)

When you run the program the output will be,

1
2
3
4

By default, Python prints everything in a new line. But in languages like C/C++ in not by default. To customize the output format Python's print() method has some parameters. They are :

  1. end
  2. sep

end parameter

This is used to end a print statement with any character/string. If we want to print the above example horizontally, we can use the end parameter.

Example

a = [1,2,3,4]
for i in a:
  print(i, end=" ")

The output will be

  1 2 3 4

sep parameter

Used to change the separator between the arguments in print(). By default Python uses space as a separator, you can change its character, integer, etc using this parameter.

Example

print( 'a' , 'b' , 'c')

The output will be

a b c

If you want to separate each argument with some symbol, you can use the sep parameter

print( 'a' , 'b' , 'c' , sep = "$")

The output will be

a$b$c
Pinninti Sagar's photo

Nice website brother...😉🔥🔥

Karri Nikhila Bhuvana Chandhrika's photo

Nice website 👌👏👏