1# %s is used as a placeholder for string values you want to inject
2# into a formatted string.
3
4# %d is used as a placeholder for numeric or decimal values.
5
6# For example (for python 3)
7
8print ('%s is %d years old' % ('Joe', 42))
9# Would output
10
11>>>Joe is 42 years old
1name = "Gandalf"
2extendedName = "the Grey"
3age = 84
4IQ = 149.9
5print('type(name):', type(name)) #type(name): <class 'str'>
6print('type(age):', type(age)) #type(age): <class 'int'>
7print('type(IQ):', type(IQ)) #type(IQ): <class 'float'>
8
9print('%s %s\'s age is %d with incredible IQ of %f ' %(name, extendedName, age, IQ)) #Gandalf the Grey's age is 84 with incredible IQ of 149.900000
10
11#Same output can be printed in following ways:
12
13
14print ('{0} {1}\'s age is {2} with incredible IQ of {3} '.format(name, extendedName, age, IQ)) # with help of older method
15print ('{} {}\'s age is {} with incredible IQ of {} '.format(name, extendedName, age, IQ)) # with help of older method
16
17print("Multiplication of %d and %f is %f" %(age, IQ, age*IQ)) #Multiplication of 84 and 149.900000 is 12591.600000
18
19#storing formattings in string
20
21sub1 = "python string!"
22sub2 = "an arg"
23
24a = "i am a %s" % sub1
25b = "i am a {0}".format(sub1)
26
27c = "with %(kwarg)s!" % {'kwarg':sub2}
28d = "with {kwarg}!".format(kwarg=sub2)
29
30print(a) # "i am a python string!"
31print(b) # "i am a python string!"
32print(c) # "with an arg!"
33print(d) # "with an arg!"