1

I'm new Python and getting a KeyError trying to use the format() function:

def hello( name="Sean", age=0 ):
    return 'Hello, ' + name + " you are { age } years old".format( age )

sentence1 = hello( 'Mark', 17 )

Error:

KeyError: ' age '

What am I doing wrong?

8 Answers 8

5

If you use a a named placeholder in the format string, you need to provide named arguments.

And you shouldn't have spaces around the name in the placeholder.

def hello( name="Sean", age=0 ):
    return 'Hello, ' + name + " you are {age} years old".format(age=age)
Sign up to request clarification or add additional context in comments.

5 Comments

Barmar, I tried your suggestion but still get the same exact result, KeyError: ' age '. What's wrong with it? I want to use this approach instead of f-strings.
Get rid of the spaces in { age }
Same result: KeyError: ' age ' Please check your code. I'll accept your answer when it runs.
I tested this, I don't get that error. You only get the error with the spaces.
Thank you Barmar, it DOES work. My server had disconnected. This was the answer I was looking for. Thank you for not using f-strings.
4

Just use a modern f string approach.

def hello( name="Sean", age=0 ):
    return f'Hello, {name} you are {age} years old.'

3 Comments

This doesn't explain why the original code didn't work.
@Barmar - you are right. But the original code is a mix of string concatenation using + and format so it looked to me it needs a different approach.
It looks like he's using .format() for age because it's an int rather than a string, so it can't be concatenated.
0

Remove the word 'age' in between the curly brackets. As such

def hello( name="Sean", age=0 ):
    return 'Hello, ' + name + " you are {} years old".format( age )

sentence1 = hello( 'Mark', 17 )

Comments

0

Just use f strings.

def hello( name="Sean", age=0 ):
    return (f'Hello, {name} you are {age} years old.')

sentence1 = hello( 'Mark', 17 )
print(sentence1)

2 Comments

This doesn't answer the question, it shows a completely different way to do the same thing.
Fair enough, however, I feel like f strings are more pythonic overall, not to mention easier to implement.
0

I suggest using f-strings: they allow you to format directly in your string. Do notice that you have to put an f before the quotation marks.

print(f"Hello {name} you are {age} years old")

Comments

0

If you want to format your text and fill a string with some variables, you can put them in format method "in your desired" order.
if there is just one variable, you can change your code to this:

def hello( name="Sean", age=0 ):
    return 'Hello, ' + name + " you are {} years old".format(age)

sentence1 = hello( 'Mark', 17 )

Also this one works:

def hello( name="Sean", age=0 ):
    return 'Hello, ' + name + " you are {age} years old".format(age=age)

sentence1 = hello( 'Mark', 17 )

In some cases, you maybe want to have multiple variables. You can use second method to your code be more readable.

Also using f-string is on the table (recommended). like this:

def hello( name="Sean", age=0 ):
    return f"Hello, {name} you are {age} years old"

sentence1 = hello('Mark', 17)

Comments

0

remove a space and pass age variable to format

"you are {age} years old".format(age=age)

def hello( name="Sean", age=0 ):
    return 'Hello, ' + name + " you are {age} years old".format(age=age)

sentence1 = hello( 'Mark', 17 )

Comments

-1

When you use .format, the curly brakets has to be empty:

def hello( name="Sean", age=0 ):
    return 'Hello, ' + name + " you are {} years old".format(age)

sentence1 = hello( 'Mark', 17 )

You sould consider using f"" as so:

return f'Hello, {name} you are {age} years old'

This way you can directly put variables in your string

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.