Python print() Function
§ The print() function prints the specified message
to the screen, or other standard output device.
§ The message can be a string, or any other object.
First Form Of print() Method
§ We
can use print() without any arguments.
What
is the purpose of print() without any arguments
§ To
insert a new line separator
print()
§ This will produce an invisible newline
character, which in turn will cause a blank line to appear on your screen.
print("java")
print("python")
#output will be
java
python
Ø
If
we want insert new line character between both statement then we should go
print () without any arguments
print("java")
print()
print("python")
#output will be
java
python
|-
print(string) method with string arguments
print("java")
print("python")
#output will be
java
python
§ If we are using
print(string) method with string arguments then escape character is allowed to
use.
print("java\n python")
print("java\tpython")
#output will be
java
python
java python
§ whenever we are
using * operator with string type then one argument should be string type and
other should be int type.
print("python"*3)
#output
will be pythonpythonpython
Print() method with multiple arguments
print(arg1,arg2,agr2,.....argn)
§ whenever we want
to display multiple argument value at a time using print() method then each
argument seperated with comma operator.
§ suppose if we want
to display two arguments at a time then we are using print() in follwing ways.
print("java","python")
#output will be java python
§ print(arg1,arg2)
then it will display first arguments value then space then second aruments
value.
§ what is difference between
two statements
print("java"+"python") #output
will be javapython
print("java","python") #java
python
print()
with variable number of arguments
a,b,c=10,20,30
print(a,b,c) #output
will be 10 20 30
§ First display the value of a variable then space then value of b variable then space then value of c variable.
Parameters inside the Print function i.e. Sep and End
print(<variable-name or
string,sep=<seperator-value>,end=<end-value>)
using sep attribute we can fulfill all customize type requirement.
Sep Parameter in
Python
§ As the name
suggests, sep is used for adding separators into the string provided.
§When we print more value than one, then how should the value be separated from
§When we print more value than one, then how should the value be separated from
each other.
§ Using sep attribute
in print() function we can specify customize separator between values.
§ If we don’t
specify any value to the “sep” parameter the by default it is considered to be
a no-space.
§ Display three variable value and each value separated with comma
a,b,c=10,20,30
print(a,b,c,sep=",")
#output will be 10,20,30
a,b,c=10,20,30
print(a,b,c,sep=":")#output will be 10:20:30
§ Display date value and each value separated with slash(/)
#for formatting today's date using backslash
print('26','07','2020', sep='/')
#output will be 26/07/2020
§ Display Two value and each value separated with @
#formatting using @ symbol
print('Scott','Tiger',sep='@')
#output will be Scott@Tiger
v print () method with end attribute: -
§ When we are using more than one print statement, then what should be the separator between them.
§ By default, the print method ends with a newline. This means there is no need to explicitly specify the parameter end as '\n.
§ But if we want to customize then we can customize it by using end attribute in print() function.
§ The end parameter is used to append any string at the end of the output of the print statement in python.
print(object) can take any type argument.
l=[10,20,30,40,50]
t=(10,20,30,40,50)
s={10,20,30,40,50}
print(l)
print(t)
print(s)
output:
[10, 20, 30, 40, 50]
(10, 20, 30, 40, 50)
{40, 10, 50, 20, 30}
§ right now here All the lines are being printed in a separate line. but if we want to
print them all in one line then we have to use the end attribute in print() function.
l=[10,20,30,40,50]
t=(10,20,30,40,50)
s={10,20,30,40,50}
print(l,end=' ')
print(t,end=' ')
print(s,end=' ')
output:
[10, 20, 30, 40, 50] (10, 20, 30, 40, 50) {40, 10, 50, 20, 30}
# ends the output with '#' hash
print('Welcome')
print('To')
print('Gravity4Tech')
output:
Welcome
To
Gravity4Tech
§ right now, here All the lines are being printed in a separate line. but if we want to print them all in one end with # hash then we have to use the end attribute in print() function.
# ends the output with '#' hash
print('Welcome',end='#')
print('To',end='#')
print('Gravity4Tech')
output:
Welcome#To#Gravity4Tech
print() function with replacement operator.
{} => replacement operator
Python
String format()
"formated
string".format(val1,val2,val3...)
'var1= {}, var= {}, var3= {}’. format (val1, val2, val3)
§ In the given formatted string Curly Braces are exchanged with the pass value in the format method.
§ The first curly braces will be exchanged with the first value, the second curly braces will be exchanged with the second value, in the same way the rest will be.
§ Each curly brace has an index position. index position starts with zero.
§ The curly braces are just placeholders for the arguments to be placed.
In the
above example, {0} is placeholder for 10 and {1} is placeholder for 20 and {2}
is placeholder for 30.
Specifying the index position is also optional.
Python String format() with keywords argument.
§ We've used the same example from above to show the difference between
keyword and positional arguments.
§ Here, instead of just the parameters, we've used a key-value for the
parameters. Namely, name="scott" and age=50.
§ Since, these parameters are referenced by their keys as {name} and {age},
they are known as keyword or named arguments.
§ Internally, the placeholder {name} is replaced by the value of name -
"scott". Since, it doesn't contain any other format codes,
"Adam" is placed.
§ For the argument age=50, the placeholder {age} is replaced by the value
50.
msg="Hello
{name},your age
is:{age} years".format(name='Scott',age=50)
print(msg)
output:
Hello
Scott,your age is:50 years
Number Formatting Types
Type Meaning
d Decimal integer
c Corresponding Unicode
character
b Binary format
o Octal format
x Hexadecimal format (lower
case)
X Hexadecimal format (upper
case)
n Same as 'd'. Except it
uses current locale setting for number separator
e Exponential notation.
(lowercase e)
E Exponential notation
(uppercase E)
f Displays fixed point
number (Default: 6)
F Same as 'f'. Except
displays 'inf' as 'INF' and 'nan' as 'NAN'
g General format. Rounds
number to p significant digits. (Default precision: 6)
G Same as 'g'. Except
switches to 'E' if the number is large.
% Percentage. Multiples by
100 and puts % at the end.
|
Display
formatting for octal, binary and hexadecimal
format.
# octal, binary and
hexadecimal format
print("bin:{0:b},oct:{0:o},hex:{0:x}".format(15))
output:
bin:1111,oct:17,hex:f
Display
formatting for % Percentage. Multiples
by 100 and puts % at the end..
#% percentage
print("Your
score is {:%}".format(3))
output:
Your
score is 300.000000%
Display
formatting of class members using format()
#define Student Class
class Student:
def __init__(self):
self.rollno=101
self.name='john'
# format rollno
print("{s.name}'s
Rollno is: {s.rollno}".format(s=Student()))
output:
john's Rollno is: 101
§ Here, Student object is passed as a keyword argument s.
§ Inside the template string,Student's name and rollno are accessed using
.name and .rollno respectively.
Display
formatting of dictionary members using format()
# define Student
dictionary
student = {'rollno': 101, 'name': 'John','course':'B.tech'}
# format student
print("{s[name]}'s
rollno is: {s[rollno]} and course is:{s[course]}".format(s=student))
output:
John's
rollno is: 101 and course is:B.tech
§ Similar to class, student dictionary is passed as a keyword argument s.
§ Inside the template string, student's name and rollno and course are accessed
using [name] and [rollno] [course] respectively.
Post a Comment