How to print both strings in a dictionary in Python

Your problem is that you need to use square brackets([]) instead of parenthesis(()). Like so:

for email in contact_emails:
    print('%s is %s' % (contact_emails[email], email)) # notice the []'s

But I recommend using the .items()( that would be .iteritems() if your using Python 2.x) attribute of dicts instead:

for name, email in contact_emails.items(): # .iteritems() for Python 2.x
    print('%s is %s' % (email, name))

Thanks to @PierceDarragh for mentioning that using .format() would be a better option for your string formatting. eg.

print('{} is {}'.format(email, name))

Or, as @ShadowRanger has also mentioned that taking advantage of prints variable number arguments, and formatting, would also be a good idea:

print(email, 'is', name)

Leave a Comment

tech