# Please help: Key Error: ' '

**URL:** <https://discuss.python.org/t/please-help-key-error/9168>\
**Category:** Python Help\
**Tags:** help\
**Created:** [June 8, 2021, 8:51pm UTC](https://discuss.python.org/t/please-help-key-error/9168 "2021-06-08T20:51:51Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Newbie21](https://avatars.discourse-cdn.com/v4/letter/n/51bf81/32.png) [@Newbie21](https://discuss.python.org/u/Newbie21)\
**Post date:** [June 8, 2021, 8:51pm UTC](https://discuss.python.org/t/please-help-key-error/9168/1 "2021-06-08T20:51:51Z")

</div>

Hello  
I am new to learning the Python language and I am currently teaching myself by going through a ten-week bootcamp book for beginners.  
I have encounter this error: Key Error: ’ ’  
The book utilises Python 3.7 and I have installed Python 3.8.8 (not sure if the escape character for tab “\t” differs between the two…)  
I have searched the internet using Google but I have been unable to find information and a specific solution. Thank you in advance for you valuable feedback.

**This is my code:**

_# create a basic receipt printing program_  
_# create a product and price for three items_  
p1\_name, p1\_price = “Books” , 49.95  
p2\_name, p2\_price = “Computer”, 579.99  
p3\_name, p3\_price = “Monitor”, 124.89

_# create a company name and information_  
company\_name = “coding temple, inc.”  
company\_address = “283 Franklin St.”  
company\_city = “Boston, MA”

_# print company information first, using format_  
print("\t\t{ }".format( company\_name.title() ) )  
print("\t\t{ }".format(company\_address) )  
print("\t\t{ }".format(company\_city) )

* * *

KeyError Traceback (most recent call last)  
 in   
3  
4 # print company information first, using format  
----\> 5 print("\t\t{ }".format( company\_name.title() ) )  
6 print("\t\t{ }".format(company\_address) )  
7 print("\t\t{ }".format(company\_city) )

KeyError: ’ ’

---

<div class="post-metadata">

**Author:** ![jeanas](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jeanas/32/8115_2.png) [@jeanas](https://discuss.python.org/u/jeanas)\
**Post date:** [June 8, 2021, 8:56pm UTC](https://discuss.python.org/t/please-help-key-error/9168/2 "2021-06-08T20:56:03Z")

</div>

Interesting. Maybe a better message could be output in this case. You don’t want a space between the curly brackets in your format strings.

---

<div class="post-metadata">

**Author:** ![Newbie21](https://avatars.discourse-cdn.com/v4/letter/n/51bf81/32.png) [@Newbie21](https://discuss.python.org/u/Newbie21)\
**Post date:** [June 8, 2021, 9:00pm UTC](https://discuss.python.org/t/please-help-key-error/9168/3 "2021-06-08T21:00:54Z")

</div>

Thank you! It is running smoothly now  
I had a strong feeling that the solution would be verrry simple…but I over-thought the problem. Why should there be no space in the curly brackets?

The author had examples in his book and his curly brackets had spaces. Could it have something to do with the version?

Also, how do you read the Key Error message to understand it better?

---

<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:** [June 8, 2021, 11:12pm UTC](https://discuss.python.org/t/please-help-key-error/9168/4 "2021-06-08T23:12:07Z")

</div>

Often they are not very informative, but they almost always recite the  
key which was not found.

If you’d post the whole KeyError message _and_ the traceback which came  
with it we can walk you through what information is there and how to use  
it.

Since you know what caused it, making that again should be as easy as  
writing a 1 or 2 line programme which causes it to happen with a “blah {  
} blah” format string.

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

---

<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:** [June 9, 2021, 12:06am UTC](https://discuss.python.org/t/please-help-key-error/9168/5 "2021-06-09T00:06:23Z")

</div>

> Thank you! It is running smoothly now  
> I had a strong feeling that the solution would be verrry simple…but I over-thought the problem. Why should there be no space in the curly brackets?

That is a good question which lacks a good answer in the Python  
documentation.

My personal feeling is that it is likely that the implementation is  
looking at the bit between the “{ }” and treating it as follows:

- _empty_ string - an implied positional value, so the first “{}” means  
the first position value in your arguments to format().

- digits: indicating a particular positional value eg “{0}” means\_  
the first position value in your arguments to format().

- identifier (and following .attr or [index] suffixes): look up  
identifier in the keyword arguments to format(), then follow whatever  
.attr or [index] things might be added  
eg {foo.x[1]} looks up “foo” in the keyword arguments, looks up .x on  
that indexes that in turn with 1.

- falls back to the syntax for “index” above

The Python docs _do not say_ that last step, which I am inferring.

If you go to the Python docs at: [3.12.1 Documentation](https://docs.python.org/3/)  
and go to: the index (top right), “F” (for “format”), and find “format”  
in the listing and follow the “(str method)” link (because you’re using  
the str.format method in your code), you land here:

```
https://docs.python.org/3/library/stdtypes.html#str.format

```

That refers to the Format String Syntax, which links to here:

```
https://docs.python.org/3/library/string.html#formatstrings

```

which says that the “arg\_name” is either an identifier or digits (cases  
3 and 2 above respectively). But what about “{ }”?

That’s a space between the brackets, so I think it is treating that as  
the string " ". In the “[element\_index]” part of that grammar it says  
that element\_index may be index\_string, and index\_string is defined as:

```
index_string ::= <any source character except "]"> +

```

This actually lets you write stuff like:

```
{foo[bare string here]}

```

to index foo with “bare string here”.

I _think_ that if the bit between “{}” is not (a) empty (b) an  
identifier or (c) digits then Python is falling back to treating it like  
index\_string, and looking that string up in the keyword arguments.

You’ve got no keyword arguments, so " " fails to be found, so the  
KeyError.

The Python format string documentation is complicated and broken up into  
several pieces.

> The author had examples in his book and his curly brackets had spaces.  
> Could it have something to do with the version?

It could. This may represent a change to the implementation of format  
strings over time. Notice that the above docs have some “Changed in  
version” bits. Those notes are added when things change, but clearly are  
not always added (tiny changes happen al the time in the code, so the  
noise in the docs would be extreme).

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

---

<div class="post-metadata">

**Author:** ![steven.daprano](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/steven.daprano/32/1083_2.png) [@steven.daprano](https://discuss.python.org/u/steven.daprano)\
**Post date:** [June 9, 2021, 1:15am UTC](https://discuss.python.org/t/please-help-key-error/9168/6 "2021-06-09T01:15:39Z")

</div>

Thanks Lin,

I have reported this as an Enhancement request for version 3.11.

[https://bugs.python.org/issue44355](https://bugs.python.org/issue44355)
