Table of Contents
show
String format operator is %
Example
>>> print("I like %s" %'python')
I like python
>>> fruit = "mango"
>>> print("I like %s" %fruit)
I like mango
>>> mark1 = 100
>>> print("My mark is %d"%mark1)
My mark is 100
>>> print("Score is %d"%128)
Score is 128
Some format operators
Signed integer decimal
Syntax
%d or % I
Example
>>> a6 = 64
>>> print("%d"%a6)
64
>>> print("%i"%a6)
64
Unsigned octal
Syntax
%o
Example
>>> a = 11
>>> print("%o"%a)
13
>>> a7 = -8
>>> print("%o"%a7)
-10
Unsigned decimal
Syntax
%u
Example
>>> a9 = 90
>>> print("%u"%a9)
90
Unsigned hexadecimal (lowercase)
Syntax
%x
Example
>>> b = 11
>>> print("%x"%b)
B
Unsigned hexadecimal (uppercase)
Syntax
%X
Example
>>> b = 11
>>> print("%X"%b)
B
Floating point exponential format (lowercase)
Syntax
%e
Example
>>> b=11
>>> print("%e"%b)
1.100000e+01
Floating point exponential format (uppercase)
Syntax
%E
Example
>>> b=11
>>> print("%E"%b)
1.100000E+01
Floating point decimal format (lower case)
Syntax
%f
Example
>>> print("%f"%b)
11.000000
>>> print("%F"%b)
11.000000
Floating point decimal format (upper case)
Syntax
%F
Example
>>> print("%F"%b)
11.000000
String
Syntax
%s
Example
>>> a1 = "cat"
>>> print("%s"%a1)
cat
Single character (int or char)
Syntax
%c
Example
>>> a2 = "c"
>>> print("%c"%a2)
c
>>> a4 = 67
>>> print("%c"%a4)
C
If more than format sequence, then that has to be printed in the tuple. The number of format sequence and the number of elements in tuple must match, Else it will produce error
>>> print("My marks in %s is %d"%('python',90))
My marks in python is 90
Views: 0