1

I have the following code:

ex_dict={1:"how",3:"do you",7:"dotoday"}
for key in ex_dict:
    string= key," and this is ", ex_dict[key]
    print (string)

The output is:

(1, ' and this is ', 'how')
(3, ' and this is ', 'do you')
(7, ' and this is ', 'dotoday')

My intended output:

1 and this is how
3 and this is do you
7 and this is dotoday

I can't seem to figure out how to get rid of the dictionary formatting in my output.

NightShadeQueen
  • 3,284
  • 3
  • 24
  • 37
user2883071
  • 960
  • 1
  • 20
  • 50

5 Answers5

5

Just use + to combine strings:

string = str(key) + " and this is " + ex_dict[key]

Since key is an integer, and you can only concatenate strings using the + operator, you should convert key to string as well.

Andrzej Pronobis
  • 33,828
  • 17
  • 76
  • 92
  • oh.. well that's embarrassing.. so when I do it using commas, it means it will add (sort of) an entry into the list? – user2883071 Sep 04 '15 at 21:09
  • The comma operator is used to define a tuple. See https://docs.python.org/2/tutorial/datastructures.html#tuples-and-sequences – Andrzej Pronobis Sep 04 '15 at 21:11
  • Commas are used to define a tuple. As you discovered, the parentheses in `string = (key, " and this is ", ex_dict[key])` are optional. Note that their use in the Python 2 `print` statement are part of that statement's syntax, and not the tuple constructor. – chepner Sep 04 '15 at 21:11
3

You're not looking at dictionary formatting, you're looking at tuples.

In Python, multiple-element tuples look like:

1,2,3

The essential elements are the commas between each element of the tuple. Multiple-element tuples may be written with a trailing comma, e.g.

1,2,3,

but the trailing comma is completely optional. Just like all other expressions in Python, multiple-element tuples may be enclosed in parentheses, e.g.

(1,2,3)

or

(1,2,3,)

Use string formatting instead:

ex_dict={1:"how",3:"do you",7:"dotoday"}
for key in ex_dict:
    print("{} and this is {}".format(key,ex_dict[key]))

Additionally: You can simplify your code with

ex_dict={1:"how",3:"do you",7:"dotoday"}
for key, val in ex_dict.iteritems(): #ex_dict.items() in python3
    print("{} and this is {}".format(key,val))

Finally: be forewarned that dictionaries in python have arbitrary order. If you want your ordering to be always the same, it's safer to use collections.OrderedDict. Because otherwise, you're depending on implementation detail. Want to see it for yourself? Make ex_dict = {1:"how",3:"do you",7:"dotoday", 9:"hi"}.

import collections
ex_dict= collections.OrderedDict([(1,"how"),(3,"do you"),(7,"dotoday")])
for key, val in ex_dict.iteritems(): #ex_dict.items() in python3
    print("{} and this is {}".format(key,val))
Community
  • 1
  • 1
NightShadeQueen
  • 3,284
  • 3
  • 24
  • 37
0

replace

string= key," and this is ", ex_dict[key]

with

string= str(key) + " and this is " + ex_dict[key]

in python you concat string with + not with ,

, is used for tuples

DorElias
  • 2,243
  • 15
  • 18
0

Replace

string= key," and this is ", ex_dict[key]

with

string= str(key) +" and this is "+ ex_dict[key]
SimoV8
  • 1,382
  • 1
  • 18
  • 32
0

You can use str.join and str.format which is more readable and less prone to simple errors:

ex_dict = {1: "how",3: "do you",7: "dotoday"}
print("\n".join(["{} and this is {}".format(*tup) 
                for tup in ex_dict.items()]))

Output:

1 and this is how
3 and this is do you
7 and this is dotoday

Or using python3 or with from __future__ import print_function with python2, you can unpack and use the sep keyword:

ex_dict = {1: "how", 3: "do you", 7: "dotoday"}
print(*("{} and this is {}".format(*tup) 
        for tup in ex_dict.items()), sep="\n")

Output:

1 and this is how
3 and this is do you
7 and this is dotoday
Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321