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 128Some format operators
Signed integer decimal
Syntax
%d or % IExample
>>> a6 = 64
>>> print("%d"%a6)
64
>>> print("%i"%a6)
64Unsigned octal
Syntax
%oExample
>>> a = 11
>>> print("%o"%a)
13
>>> a7 = -8
>>> print("%o"%a7)
-10Unsigned decimal
Syntax
%uExample
>>> a9 = 90
>>> print("%u"%a9)
90Unsigned hexadecimal (lowercase)
Syntax
%xExample
>>> b = 11
>>> print("%x"%b)
BUnsigned hexadecimal (uppercase)
Syntax
%XExample
>>> b = 11
>>> print("%X"%b)
BFloating point exponential format (lowercase)
Syntax
%eExample
>>> b=11
>>> print("%e"%b)
1.100000e+01Floating point exponential format (uppercase)
Syntax
%EExample
>>> b=11
>>> print("%E"%b)
1.100000E+01Floating point decimal format (lower case)
Syntax
%fExample
>>> print("%f"%b)
11.000000
>>> print("%F"%b)
11.000000Floating point decimal format (upper case)
Syntax
%FExample
>>> print("%F"%b)
11.000000String
Syntax
%sExample
>>> a1 = "cat"
>>> print("%s"%a1)
catSingle character (int or char)
Syntax
%cExample
>>> a2 = "c"
>>> print("%c"%a2)
c
>>> a4 = 67
>>> print("%c"%a4)
CIf 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 90Views: 0