For loop dictionary "%" in syntax | Python Language Forum
H
Heena Goyal Posted on 01/12/2023
print("Dictionary Iteration") 
d = dict() 
d['xyz'] = 123
d['abc'] = 345
for i in d: 
    print("%s  %d" % (i, d[i]))

I have came across the above syntax on internet, please explain the synatx in print line.

Y
Yogesh Chawla Replied on 01/12/2023

They are used for formatting strings. 
%s acts a placeholder for a string while %d acts as a placeholder for a number. 
Their associated values are passed in via a tuple using the % operator.
 
For example
name = 'abc'
number = 42
print '%s %d' % (name, number)
 
will print abc 42. Note that name is a string (%s) and number is an integer (%d for decimal).
 
In Python 3, the same example becomes:
 
print('%s %d' % (name, number))
 
That's the reason, in your code, the output is
xyz  123
abc  345