-6

I have the following:

max_id = 10
    for i in range(max_id):
        payload = "{\"text\": 'R'+str(i),\"count\":\"1 \",}"
        print(payload)

I want to iterate through this , and have the value of text be set to "R1", "R2" ... Upon debugging the output is:

{"text": 'R'+str(i),"count":"1",}

What am I doing wrong?

3
  • 8
    Double-check your double quotes. The syntax highlighting in your question might give you a hint. Commented May 31, 2016 at 14:46
  • 4
    String interpolation in python Commented May 31, 2016 at 14:47
  • Based on stackoverflow.com/questions/4450592/…, I've come up with payload = "{\"text\": %s,\"count\":\"1 \"}" % ("R"+str(i)) -Thank you Commented May 31, 2016 at 15:12

1 Answer 1

2
for i in range(max_id):
    payload = "{\"text\": R"+str(i)+",\"count\":\"1 \",}"
    print(payload)

Problem with your double quotes.

Output:

{"text": R0,"count":"+i+ ",}
{"text": R1,"count":"+i+ ",}
{"text": R2,"count":"+i+ ",}
{"text": R3,"count":"+i+ ",}
{"text": R4,"count":"+i+ ",}
{"text": R5,"count":"+i+ ",}
{"text": R6,"count":"+i+ ",}
{"text": R7,"count":"+i+ ",}
{"text": R8,"count":"+i+ ",}
{"text": R9,"count":"+i+ ",}

My be you are looking for this one.

for i in range(10):
    payload = "{\"text\": R%s,\"count\":\"1 \",}" %(i)
    print(payload)
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks , I think people wanted the more pythonic method, please see my comment. Your way does work though
@user61629 Updated the question. Please check it.
It's easier to read and write if you use single quotes as delimiters: payload = '{"text": R%s,"count":"1 ",}" %(i)
I'm working with windows so escaping helps
Thanks Rahul, that helps

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.