-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?

user1592380
  • 34,265
  • 92
  • 284
  • 515
  • 8
    Double-check your double quotes. The syntax highlighting in your question might give you a hint. – Wander Nauta May 31 '16 at 14:46
  • 4
    [String interpolation in python](http://stackoverflow.com/questions/4450592/string-interpolation-in-python) – C.B. May 31 '16 at 14:47
  • Based on http://stackoverflow.com/questions/4450592/string-interpolation-in-python, I've come up with payload = "{\"text\": %s,\"count\":\"1 \"}" % ("R"+str(i)) -Thank you – user1592380 May 31 '16 at 15:12

1 Answers1

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)
Rahul K P
  • 15,740
  • 4
  • 35
  • 52