Python print() function

Python | print() function: In this tutorial, we are going to learn about the print() function with parameters and examples.
Submitted by IncludeHelp, on June 14, 2020

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.

Example 1: Printing text, values, etc (i.e. without using optional parameters)

# 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

Example 2: print() with sep parameter

# 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

Example 3: print() with end parameter

# 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]@@

Example 4: print() with file parameter

# 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?

Example 5: print() with flush parameter

# 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.






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





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.