Python print() Function

 

Python print() Function

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

Let's explain its parameters one by one.

  • objects - An object is nothing but a statement that to be printed. The * sign represents that there can be multiple statements.
  • sep - The sep parameter separates the print values. Default values is ' '.
  • end - The end is printed at last in the statement.
  • file - It must be an object with a write(string) method.
  • flush - The stream or file is forcibly flushed if it is true. By default, its value is false.

Python Program

#Example - 1: Return a value
print("Welcome to javaTpoint.")  
  
a = 10  
# Two objects are passed in print() function  
print("a =", a)  
  
b = a  
# Three objects are passed in print function  
print('a =', a, '= b')

print ("=================================================================")



#Example - 2: Using sep and end argument

a = 10  
print("a =", a, sep='dddd', end='\n\n\n')  
print("a =", a, sep='0', end='$$$$$')
print ("=================================================================")



#Taking Input to the User: Python provides the input() function which is used to take input from the user. Let's understand the following example.
#Example - 3 

print (sep='dd')
name = input("Enter a name of student:")  
print("The student name is: ", name)
print ("=================================================================")



#Example - 4

a  = int(input("Enter first number: "))  
b = int(input("Enter second number: "))  
print("the sum of a+b is:", a+b)  


Output:

Welcome to javaTpoint.
a = 10
a = 10 = b
=================================================================
a =dddd10


a =010$$$$$=================================================================

Enter a name of student:prabhu
The student name is:  prabhu
=================================================================
Enter first number: 10
Enter second number: 20
the sum of a+b is: 30



=======================================================================
Another EXAMPLE : Single /Multiple lines printing
=======================================================================


We can print multiple variables within the single print statement. Below are the example of single and multiple printing values.


#Example - 1 (Printing Single Variable)
# printing single value 
a=5
print(a)
print("a value is ", a)


#Example - 2 (Printing Multiple Variables)
a=5
b=6
print(a,b)
#separate the variables by the comma  
print(10,20,30,40,50,60)



OUTPUT:

5
a value is  5
5 6
10 20 30 40 50 60