In [8]:
print("My number %s" %("hello"))
In [11]:
s=13.4576
print('Floating point: %1.3f' %(s))
print('FLoating point value appending zeros: %1.8f' %(s))
print('Floating point with filled in spaces: %25.4f' %(s))
%x.yf format: x->Minimum number of characters for the string, y-> number of digits after the decimal point.
You can print multiple objects or values at same time.
In [7]:
print('First: %s, Second: %1.1f and Third: %s' %(1,2.0,"three"))
The values will follow order of appearance (same as in C's printf())
%s and %r are 2 conversion methods, they convert any python object to a string.
%s uses str() and %r uses repr().
In [20]:
print('This is object to string using %s & %s' %(1234,"String here!"))
In [22]:
print('This is object to string using %r & %r' %(4321,"String here!"))
In [13]:
a=2
b=3
c=a+b
print('The sum is: {res}'.format(res=c))
In [14]:
print('First: {x}, Second: {y} and Third: {z}'.format(x=1, y=2.0, z="three"))
You can print the same value more than once.
In [15]:
print('First: {x}, Second: {y} and again First: {x}'.format(x=1, y=2))
No need to worry about the order here.
In [ ]: