=================================================================
Display the Result of Python Program on the Console
OR
Ouput Statement in Python
=================================================================
=>To Display the Result of Python Program on the Console, we use a Pre-defined
Function called print().
=>In otherwords, print() is a Pre-defined Function used for displying the Result of
Python on the Console.
=>The print() can be used in 7 ways:
-----------------------------------------------------------------------------------
----------------------------------------------------------------------------
Syntax-1: print(val1)
OR
print(val1,val2,....,val-n)
-----------------------------------------------------------------------------------
----------------------------------------------------------------------------
=>This Syntax used for Displying either One Value OR Multiple Values.
---------------------
Examples
----------------------
>>> a=10
>>> print(a)------------------10
>>> a=10
>>> b=20
>>> c=a+b
>>> print(a,b,c)--------------10 20 30
-----------------------------------------------------------------------------------
----------------------------------------------------------------------------
Syntax-2: print(msg)
OR
print(msg1,msg2,....,msg-n)
OR
print(msg1+msg2+....+msg-n)
-----------------------------------------------------------------------------------
----------------------------------------------------------------------------
=>Here msg1,msg2,....,msg-n Represents String Data
=>This Syntax displays the Messages on the console
=>We can also use Operator + for Concatination of multiple str values
-------------------------
Examples
-------------------------
>>> print("Hello Python world")---------------Hello Python world
>>> print("Hello","Python","world")---------Hello Python world
>>> print("Hello",'Python',"world")---------Hello Python world
>>> print("Hello"+'Python'+"world")-------HelloPythonworld
>>> print("Hello"+" "+"Python"+" "+"world")---Hello Python world
-----------------
>>> print("10"+"34")---------------------------1034
>>> print("10"+34)-----------------------------TypeError: can only concatenate str
(not "int") to str
>>> print(int("10")+34)---------------------44
>>> print("10"+str(34) )--------------------1034
-----------------------------------------------------------------------------------
----------------------------------------------------------------------------
Syntax-3: print(Message Cum Value)
OR
print(Value Cum Message)
-----------------------------------------------------------------------------------
----------------------------------------------------------------------------
=>This Syntax display Message Cum Values.
---------------------------
Examples
---------------------------
>>> a=10
>>> print("Val of a=",a)---------------Val of a= 10
>>> print("Val of a="+str(a))--------Val of a=10
----------------------------
>>> print(a,"is the val of a")------------------10 is the val of a
>>> a=10
>>> b=20
>>> c=a+b
>>> print("sum=",c)------------sum= 30
>>> print(c,"is the sum")------30 is the sum
-----------------------
>>> a=10
>>> b=20
>>> c=a+b
>>> print("Sum of",a,"and",b,"=",c)-------------Sum of 10 and 20 = 30
>>> print("sum(",a,",",b,")=",c)-----------------sum( 10 , 20 )= 30
>>> print(a,"+",b,"=",c)---------------------------10 + 20 = 30
>>> print(str(a)+"+"+str(b)+"="+str(c))-------10+20=30
-----------------------------------------------------------------------------------
----------------------------------------------------------------------------
Syntax-4: print(Message Cum Value with format())
OR
print(Value Cum Message with format() )
-----------------------------------------------------------------------------------
----------------------------------------------------------------------------
=>This Syntax display Message Cum Values with format()
--------------------------
Examples
--------------------------
>>> a=10
>>> print("Val of a={}".format(a))----------------Val of a=10
>>> print("{} is the val of a".format(a))---------10 is the val of a
-------------------
>>> a=10
>>> b=20
>>> c=a+b
>>> print("Sum of {} and {}={}".format(a,b,c))-------Sum of 10 and 20=30
>>> print("sum({},{})={}".format(a,b,c))---------------sum(10,20)=30
>>> print("{}+{}={}".format(a,b,c))--------------------10+20=30
-----------------------------------------------------------------------------------
----------------------------------------------------------------------------
Syntax-5: print(f "Messages {Var1},{Var2},....{Var-n}")
-----------------------------------------------------------------------------------
----------------------------------------------------------------------------
=>This Syntax display Message Cum Values with a letter 'f'.
---------------------------
Examples
---------------------------
>>> a=10
>>> print(f"val of a={a}")--------------val of a=10
>>> print(f"{a} is the val of a")-------10 is the val of a
>>> a=10
>>> b=20
>>> c=a+b
>>> print(f"Sum of {a} and {b}={c}")----------Sum of 10 and 20=30
>>> print(f"sum({a},{b})={c}")-----------------sum(10,20)=30
-----------------------------------------------------------------------------------
----------------------------------------------------------------------------
Syntax-6: print("Message Value with Format Specifiers")
-----------------------------------------------------------------------------------
----------------------------------------------------------------------------
=>Here %d Represents Integer Data
%f Represents float data
%s Represnets Str Data
=>If we don't have Format Specifier for any value then convert that Value into str
type by using str() and use %s.
------------------------------
Examples
------------------------------
>>>a=10
>>> print("Val of a=%d" %a)-------------Val of a=10
>>> print("%d is the val of a" %a)-----10 is the val of a
------------------------
>>> a=10
>>> b=20
>>> c=a+b
>>> print("Sum of %d and %d=%d" %(a,b,c) )--------Sum of 10 and 20=30
>>> print("sum(%d,%d)=%d" %(a,b,c))---------------sum(10,20)=30
----------------------
>>> a=1.2
>>> b=2.3
>>> c=a+b
>>> print("Sum of %f and %f=%f" %(a,b,c))-----------Sum of 1.200000 and
2.300000=3.500000
>>> print("Sum of %0.2f and %0.2f=%0.3f" %(a,b,c))----Sum of 1.20 and 2.30=3.500
>>> print("Sum of %0.10f and %0.10f=%0.10f" %(a,b,c))---Sum of 1.2000000000 and
2.3000000000=3.5000000000
------------------------------
>>> a=3.456789
>>> print("Val of a=%f" %a)------------Val of a=3.456789
>>> print("Val of a=%0.2f" %a)--------Val of a=3.46
>>> a=3.455789
>>> print("Val of a=%0.2f" %a)------Val of a=3.46
>>> a=3.454789
>>> print("Val of a=%0.2f" %a)-------Val of a=3.45
-------------------------------------
>>> sno=10
>>> sname="Rossum"
>>> marks=23.45
>>> print("My Roll Number is %d and name is %s and marks is %0.2f" %
(sno,sname,marks))
My Roll Number is 10 and name is Rossum and
marks is 23.45
>>> print("My Roll Number is %d and name is '%s' and marks is %0.2f" %
(sno,sname,marks))
My Roll Number is 10 and name is 'Rossum' and
marks is 23.45
-------------------------------------------
>>> lst=[10,"RS",34.45,True]
>>> print("content of lst={}".format(lst))--------------content of lst=[10, 'RS',
34.45, True]
>>> print("content of lst=%d" %lst)--------------------TypeError: %d format: a real
number is required, not list
>>> print("content of lst=%s" %str(lst))--------------content of lst=[10, 'RS',
34.45, True]
>>> d={10:1.2,20:2.3,30:3.4}
>>> print("content of d=%s" %str(d))----------------content of d={10: 1.2, 20: 2.3,
30: 3.4}
-----------------------------------------------------------------------------------
----------------------------------------------------------------------------
Syntax-7: print(Value,end="delimter")
-----------------------------------------------------------------------------------
----------------------------------------------------------------------------
=>This Syntax displays the values in same line
--------------------------------
Examples
--------------------------------
for val in range(10,21,2):
print(val,end=" ")-----------------10 12 14 16 18 20
-----------------------------------------------------------------------------------
----------------------------------------------------------------------------