DEV Community

Cover image for The print method in Python
Manitej ⚡
Manitej ⚡

Posted on • Originally published at manitej.hashnode.dev

The print method in Python

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..")
Enter fullscreen mode Exit fullscreen mode

Example

Consider the below program,

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

When you run the program the output will be,

1
2
3
4
Enter fullscreen mode Exit fullscreen mode

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=" ")
Enter fullscreen mode Exit fullscreen mode

The output will be

  1 2 3 4
Enter fullscreen mode Exit fullscreen mode

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')
Enter fullscreen mode Exit fullscreen mode

The output will be

a b c
Enter fullscreen mode Exit fullscreen mode

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

print( 'a' , 'b' , 'c' , sep = "$")
Enter fullscreen mode Exit fullscreen mode

The output will be

a$b$c
Enter fullscreen mode Exit fullscreen mode

Top comments (6)

Collapse
 
sandordargo profile image
Sandor Dargo

Cool. I think the file parameter is also worth mentioning.

Collapse
 
manitej profile image
Manitej ⚡

Thanks for the tip 🤩

Collapse
 
janmpeterka profile image
Jan Peterka

Wow, using print all the time and didn't know about this. Thanks :)

Collapse
 
manitej profile image
Manitej ⚡

True. sep is underrated

Collapse
 
titanhero profile image
Lex

Very cool😁👍✌️

Collapse
 
manitej profile image
Manitej ⚡

Thanks lex ✋