views:

23

answers:

1

I understand everything from this code:

def display_meta(request):
    values = request.META.items()
    values.sort()
    html = []
    for k, v in values:
        html.append('<tr><td>%s</td><td>%s</td></tr>' % (k, v))
    return HttpResponse('<table>%s</table>' % '\n'.join(html))

Except this line: '\n'.join(html)

So \n creates a new line for every table I assume. But what does join(html) do?

+2  A: 

It basically puts a newline between every item in html.

So if

html = ['<!DOCTYPE html>', '<html>', '<body>', '<p>']

that piece of code will create this string:

"""
<!DOCTYPE html>
<html>
<body>
<p>
"""

http://docs.python.org/library/stdtypes.html#str.join

W_P
Nice thanks! So its dicitem1 + \n +dictitem2 +\n ...etc. And I can decide which String I want to put inbeetween? If I write: '/'.join(html) it would be?:<!DOCTYPE html>/<html>/<body>/<p>With your example?
MacPython
@MacPython Yes, it would concatenate strings with `/`.
rebus
Thanks! I just tried it and it still gave me a table with key : value pairs. The only difference was that at the top line he added /////////////////////////////////////////Which is wired. But interesting. I thought it would be one line seperated by /. Well dont want to waste your time. Thanks again!
MacPython
In your case join only concatenates table rows and you can't put content other then special characters such as `\n` (newline) between table rows.
rebus