concatenating a string to the end of a a returned json list python

Viewed 31

Hi I am using the below code to return some json from a url and it returns the results as a list just how I want, however I want to add a string to each of the returned results.

Printing n in a for loop works but I dont know where to add the string I want to add to the result, everything I have tried throws errors about mixing stings with lists. I have read about arrays and can add data to the array and show onlly certain results but am not sure where to start with adding data to the result.

for example.

I get

Result1
Result2
Result3, 
etc,

however I want to return

String Result1
String Result2
String Result3.

the string will be constant but the result will be what has been returned.

import urllib.request as request
import json


with request.urlopen('https://jsondatalist') as response:
    if response.getcode() == 200:
        source = response.read()
        data = json.loads(source)

        for n in (data):
            print (n)

    else:
        print('An error occured while attempting to retrieve data from source')
1 Answers

It seems like you are asking about string interpolation.

on python > 3.6 you can do print(f"String {n}")

on older versions of python you can do print("String {}".format(n))

Related