Issue
I have the following payload that I wish to use to make a post request. How would I reformat if I wanted each of the values for Age, Average Family Size, and City to be variables?
payload = "{\"Age\": \"50\",\n \"Average Family Size\": \"2.5\",\n \"City\": \"Washington\"}"
Solution
This is a good use for F-strings.
Assuming your variables are named age
, size
, and city
:
payload = f'{"Age": "{age}",\n "Average Family Size": "{size}",\n "City": "city"}'
Python 2.7 old-fashioned %s
string interpolation:
payload = f'{"Age": "%s",\n "Average Family Size": "%s",\n "City": "%s"}' % (age, size, city)
Also, by enclosing the entire string in single-quotes, you don't have to escape the double-quotes.
Answered By - John Gordon
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.