bigalbunyan
bigalbunyan

Reputation: 103

f string formatting: Invalid format Specifier

I'm trying to pass the following data into POST request:

data = {
    "To": MY_PHONE_NUMBER,
    "From": TWILIO_PHONE_NUMBER,
    "Parameters": f'{"appointment_time":{apointment_time}, "name":{name_var}, "age":{age_var}}',
}

I'm getting the following error:

"Parameters": f'{"appointment_time":{apointment_time}, "name":{name_var}, "age":{age_var}}',
ValueError: Invalid format specifier

I've tried using .format as well with no luck.

Upvotes: 3

Views: 4662

Answers (2)

Saeed
Saeed

Reputation: 4133

You have an extra } character at the end of your code near ``age_var` variable:

"Parameters": f'{"appointment_time":{apointment_time}, "name":{name_var}, "age":{age_var}}',

It should be:

"Parameters": f'{"appointment_time":{apointment_time}, "name":{name_var}, "age":{age_var}',

You should remove it to work.

Upvotes: -1

gph
gph

Reputation: 1359

In an f string it interprets the brace as preceding a variable. The braces you included for formatting are confusing the parser. Use two braces to escape it.

"Parameters": f'{{"appointment_time":{apointment_time}, "name": {name_var}, "age":{age_var}}}'

Upvotes: 12

Related Questions