# Dictionaries and F strings

**URL:** <https://discuss.python.org/t/dictionaries-and-f-strings/13478>\
**Category:** Python Help\
**Tags:** help\
**Created:** [February 2, 2022, 9:00pm UTC](https://discuss.python.org/t/dictionaries-and-f-strings/13478 "2022-02-02T21:00:18Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![DarkNewt](https://avatars.discourse-cdn.com/v4/letter/d/5f8ce5/32.png) [@DarkNewt](https://discuss.python.org/u/DarkNewt)\
**Post date:** [February 2, 2022, 9:00pm UTC](https://discuss.python.org/t/dictionaries-and-f-strings/13478/1 "2022-02-02T21:00:18Z")

</div>

I am studying a python course, and going back over the material. One of the examples uses a dictionary and a for loop. It uses a print statement to print out the dictionary contents and that works, however when I try to convert the print statement to an f string it fails. I have tried various combinations and I have also read through several guides on using f strings and dictionaries with no luck.

I would apreciate if anyone could tell me where I am going wrong and how I should rewrite this for loop to print out correctly:

#here is an example of a Dictionary sequence which prints both the key and value denoted as K for Key and V for value  
print (‘here is a list value of key value pairs’)  
x = { ‘one’: 1, ‘two’: 2, ‘three’: 3, ‘four’: 4, ‘five’: 5}

#I have commented out the working print statement below and tried to unsuccessfully replace it with an f’ string.

for k, v in x.items():  
#print(‘Dictionary with key and value - k: {}, v: {}’.format(k, v))  
print(f’This is a dictionary Key Pair value {k[“k”]} v{[“v”]}’)

---

<div class="post-metadata">

**Author:** ![kpfleming](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/kpfleming/32/2761_2.png) [@kpfleming](https://discuss.python.org/u/kpfleming)\
**Post date:** [February 2, 2022, 9:09pm UTC](https://discuss.python.org/t/dictionaries-and-f-strings/13478/2 "2022-02-02T21:09:58Z")

</div>

Which version of Python are you using, and what results do you get when you try the modified code?

---

<div class="post-metadata">

**Author:** ![kpfleming](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/kpfleming/32/2761_2.png) [@kpfleming](https://discuss.python.org/u/kpfleming)\
**Post date:** [February 2, 2022, 9:11pm UTC](https://discuss.python.org/t/dictionaries-and-f-strings/13478/3 "2022-02-02T21:11:39Z")

</div>

Wait… your code is not correct 🙂

`k` and `v` are already directly-usable values, you don’t need to use them as keys or indices.

```auto
print(f'This is a dictionary Key Pair value {k} {v}')

```

---

<div class="post-metadata">

**Author:** ![kpfleming](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/kpfleming/32/2761_2.png) [@kpfleming](https://discuss.python.org/u/kpfleming)\
**Post date:** [February 2, 2022, 9:12pm UTC](https://discuss.python.org/t/dictionaries-and-f-strings/13478/4 "2022-02-02T21:12:37Z")

</div>

If you want them labeled like the original code:

```auto
print(f'This is a dictionary Key Pair value k: {k}, v: {v}')

```

---

<div class="post-metadata">

**Author:** ![cameron](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/cameron/32/2658_2.png) [@cameron](https://discuss.python.org/u/cameron)\
**Post date:** [February 2, 2022, 9:52pm UTC](https://discuss.python.org/t/dictionaries-and-f-strings/13478/5 "2022-02-02T21:52:07Z")

</div>

Look at the expressions inside the {}format markers:

```
k["k"]
["v"]

```

Do they look like what you would write _outside_ a format string? I  
suspect you just want

```
k
v

```

and therefore a format string:

```
print(f'This is a dictionary Key Pair value k: {k}, v: {v}')

```

or with a current Python:

```
print(f'This is a dictionary Key Pair value {k=}, {v=}')

```

I suspect you’re overcomplicating things in your mind.

Cheers,  
Cameron Simpson [cs@cskk.id.au](mailto:cs@cskk.id.au)

---

<div class="post-metadata">

**Author:** ![DarkNewt](https://avatars.discourse-cdn.com/v4/letter/d/5f8ce5/32.png) [@DarkNewt](https://discuss.python.org/u/DarkNewt)\
**Post date:** [February 8, 2022, 12:19am UTC](https://discuss.python.org/t/dictionaries-and-f-strings/13478/6 "2022-02-08T00:19:48Z")

</div>

Hi All, thanks for this, sorry for my late reply!! I am not getting any notifications to responses. Above code worked thanks all!
