ADVERTISEMENT
ADVERTISEMENT

Python print() function

Python | print() Function: The complete guide to the Python's print() function, learn how to print text, values, and print() function with sep parameter, print() function with end parameter, print() function with file parameter, print() function with flush parameter.
Submitted by IncludeHelp, on October 02, 2021

print()

print() function is a library function in Python, it is used to print the value of the given arguments (objects) to the standard output stream i.e. to print it on the screen.

General syntax:

print(text/object)

Full syntax:

print(objects, sep=' ', end='\n', file=sys.stdout, flush=False)

Parameter(s):

  • objects: The value or the variables/objects to be printed on the screen, multiple objects can be passed separating by the commas (object1, object2, ..., objectN).
  • sep: It's an optional parameter and is used to specify the separator between the arguments, The default value is space (' ').
    Read more: sep parameter in print() function.
  • end: It's also an optional parameter and is used to specify the value to be printed at the last. The default value is a newline character ('\n').
    Read more: end parameter in print() function.
  • file: It's also an optional parameter and is used to specify the file name where we can write the argument values. The default value is sys.stdout.
    Read more: file parameter in print() function.
  • flush: It's also an optional parameter and is used to flush the stream. The default value is "False".
    Read more: flush parameter in print() function.

Return value:

The return type of print() function is NoneType, it returns nothing.

print() function to print text, values

In the below example, we will learn how to print the text, values, etc, i.e., how to use the print() function without using optional parameters?

Example 1:

# Python code to demonstrate the example of 
# print() function without using 
# optional parameters 

# printing strings
print("Hello, world!")
print("How are you?")
print("India","USA", "Russia", "Israel")
print()

# printing mixed value
print("Mike", 21, "USA", 65.50)
print([10, 20, 30]) # list
print({"Mike", 21, "USA", 123.5}) # set
print(123, "Hello", [10, 20, 30]) #number, string, list
print()

# printing text or/and variables
name = "Mike"
age = 21
con = "USA"
w = 65.50

print(name, age, con, w)
print("Name:", name, "Age:", age, "Country:", con, "Weight:", w)

Output:

Hello, world!
How are you?
India USA Russia Israel

Mike 21 USA 65.5
[10, 20, 30]
{'Mike', 123.5, 'USA', 21}
123 Hello [10, 20, 30]

Mike 21 USA 65.5
Name: Mike Age: 21 Country: USA Weight: 65.5

print() function with sep parameter

sep parameter stands for separator, it uses with the print() function to specify the separator between the arguments. The default value is space i.e. if we don't use sep parameter – then the value of the arguments is separated by the space. In the below program, we will learn how to use sep parameter with the print() function?

Example 2:

# Python code to demonstrate the example of 
# print() function with sep parameter

print("Separated by ','")
print("Mike", 21, "USA", 65.50, sep=',')
print("Separated by ' # '")
print("Mike", 21, "USA", 65.50, sep=' # ')
print()

name = "Mike"
age = 21
con = "USA"
w = 65.50

print("Separated by ','")
print(name, age, con, w, sep=',')
print("Separated by '\n'")
print("Name:", name, "Age:", age, "Country:", con, "Weight:", w, sep='\n')

Output:

Separated by ','
Mike,21,USA,65.5
Separated by ' # '
Mike # 21 # USA # 65.5

Separated by ','
Mike,21,USA,65.5
Separated by '
'
Name:
Mike
Age:
21
Country:
USA
Weight:
65.5

print() function with end parameter

Python's print() function comes with a parameter called end. By default, the value of this parameter is '\n', i.e., the new line character. We can specify the string/character to print at the end of the line. In the below program, we will learn how to use the end parameter with the print() function?

Example 3:

# Python code to demonstrate the example of 
# print() function with end parameter

print("Ended by none i.e. removing default sep value")
print("Hello,", end='')
print("world", end='')


print("Ended by '###\\n'")
print("Mike", 21, "USA", 65.50, end='###\n')
print("Ended by '-END-\\n'")
print("Mike", 21, "USA", 65.50, end='-END-\n')
print()

name = "Mike"
age = 21
con = "USA"
w = 65.50

print("Ended by 'FINISH\\n'")
print(name, age, con, w, end='FINISH\n')
print("Ended by '@@@@'")
print("Name:", name, "Age:", age, "Country:", con, "Weight:", w, end='@@@')

Output:

Ended by none i.e. removing default sep value
Hello,worldEnded by '###\n'
Mike 21 USA 65.5###
Ended by '-END-\n'
Mike 21 USA 65.5-END-

Ended by 'FINISH\n'
Mike 21 USA 65.5FINISH
Ended by '@@@@'
Name: Mike Age: 21 Country: USA Weight: [email protected]@@

print() function with file parameter

file parameter is used with print() function to write the value of given arguments to the specified file. If it is not specified, by default the value is written to system.stdout. It can be used to create log the values i.e. to keep the track to the statements, logics, etc. In the below program, we will learn how to use the file parameter with the print() function?

Example 4:

# Python code to demonstrate the example of 
# print() function with file parameter

import sys

print("Printing to sys.stderr")
print("Hello, world!", file = sys.stderr)

print("Printing to an external file")
objF = open("logs.txt", "w")
print("How are you?", file = objF)

objF.close()

Output:

Printing to sys.stderr
Hello, world!
Printing to an external file

--- logs.txt ---
How are you?

print() function with flush parameter

Python's print() method as an exclusive attribute namely, flush which allows the user to decide if he wants his output to be buffered or not. The default value of this is False meaning the output will be buffered. In the below program, we will learn how to use the flush parameter with the print() function?

Example 5:

# Python code to demonstrate the example of 
# print() function with flush parameter

from time import sleep

# output is flushed here
print("Hello, world!", end='', flush= True)
sleep(5)
print("Bye!!!")

# output is not flushed here
print("IncludeHelp", end='')
sleep(5)
print("Okay!!!")

Output:

Hello, world!Bye!!!
IncludeHelpOkay!!!

See the output – "Hello, world" and "Bye!!!" are printing correctly because before sleep(5) the print() is flushing but "IncludeHelp" and "Okay!!!" are printing together, because print() is not flushing.

ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.