0

Line of code:

data = '{"username": {u}, "password": {p}}'.format(u=user_name, p=password)

I just want to insert variables user_name and password inside the stringified dict above in the places u and p resp.

Error:

data = '{"username": {u}, "password": {p}}'.format(u=user_name, p=password)
KeyError: '"username"'

I also tried with: data = f'\{"username": {user_name}, "password": {password}\}'

But that didn't seem to work.

Any other way to tackle this?

3
  • you want to make a string representation of a dict? Commented Apr 17, 2020 at 11:00
  • I just want to insert variables inside the stringified dict. (in places of 'u' and 'p' Commented Apr 17, 2020 at 11:02
  • data = '{{"username": {u}, "password": {p}}}'.format(u=user_name, p=password) (see answer from @Masklinn) Commented Apr 17, 2020 at 11:05

2 Answers 2

3

You need to double up the braces to escape them in str.format patterns.

Though the code is odd, why are you trying to insert stuff into a stringified pseudo-dict instead of stringifying the final dict e.g. json.dumps({'username': u, 'password': p})?

Because here the content of your data items will be "raw" e.g. assuming user_name="billy bob" and password="george" you're doing to end up with

'{"username": billy bob, "password": george}'

which doesn't make much sense to me.

Sign up to request clarification or add additional context in comments.

7 Comments

I just want to pass this data variable into a requests.post('link', data=data) and the data consists of credentials username and password needed to hit an external API. So was trying to figure out if there is something we can do in shorter lines and edit the variable in such a way without using json library
@AjayBhagchandani Just to make sure: Have you seen the example in the requests documentation: r = requests.post('https://httpbin.org/post', data = {'key':'value'}). There data is passed as a dictionary, not as a string that looks like a botched representation of a dictionary.
@AjayBhagchandani there's no reason not to use the json library built into the stdlib, but here you can just use requests.post(..., json=data).
@Matthias that's going to form-encode the dict, not send it as json data. It's pretty rare for "external API" to use form-encoded inputs, though it happens.
The documentation gives an example for that too: payload = {'some': 'data'} followed by r = requests.post(url, data=json.dumps(payload)).
|
2

When you want to include {} in an f-string we simply double them up.

So the following gives the output you want.

>>> username = "foo"
>>> password = "bar"
>>> f'{{"username": {username}, "password": {password}}}'
'{"username": foo, "password": bar}'

4 Comments

You should really use single quotes as string delimiters so you don't have to escape all the double quotes in the string.
I've ammended the answer.
Worked like a charm. Got to know something new. Thanks. f'{{ "username": "{user_name}", "password": "{password}"}}'
ty for double quotes

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.