tester
tester

Reputation: 23189

How would I make the output of this for loop into a string, into a variable?

In this loop, I'm trying to take user input and continually put it in a list till they write "stop". When the loop is broken, the for loop prints out all of the li's.

How would I take the output of the for loop and make it a string so that I can load it into a variable?

x = ([])
while True:
    item = raw_input('Enter List Text (e.g. <li><a href="#">LIST TEXT</a></li>) (Enter "stop" to end loop):\n')
    if item == 'stop':
        print 'Loop Stopped.'
        break           
    else:
        item = make_link(item)
        x.append(item)
        print 'List Item Added\n'


    for i in range(len(x)):
        print '<li>' + x[i] + '</li>\n'

I want it to end up like this:

Code:

print list_output

Output:

<li>Blah</li>
<li>Blah</li>
<li>etc.</li>

Upvotes: 0

Views: 2384

Answers (5)

James Brady
James Brady

Reputation: 27482

I hate to be the person to answer a different question, but hand-coded HTML generation makes me feel ill. Even if you're doing nothing more than this super-simple list generation, I'd strongly recommend looking at a templating language like Genshi.

A Genshi version of your program (a little longer, but way, way nicer):

from genshi.template import MarkupTemplate

TMPL = '''<html xmlns="http://www.w3.org/1999/xhtml"
          xmlns:py="http://genshi.edgewall.org/">
          <li py:for="item in items">$item</li>
          </html>'''

make_link = lambda x: x

item, x = None, []
while True:
    item = raw_input('Enter List Text (Enter "stop" to end loop):\n')
    if item == 'stop':
        break
    x.append(make_link(item))
    print 'List Item Added\n'

template = MarkupTemplate(TMPL)
stream = template.generate(items = x)
print stream.render()

Upvotes: 2

zweiterlinde
zweiterlinde

Reputation: 14769

In python, strings support a join method (conceptually the opposite of split) that allows you to join elements of a list (technically, of an iterable) together using the string. One very common use case is ', '.join(<list>) to copy the elements of the list into a comma separated string.

In your case, you probably want something like this:

list_output = ''.join('<li>' + item + '</li>\n' for item in x)

If you want the elements of the list separated by newlines, but no newline at the end of the string, you can do this:

list_output = '\n'.join('<li>' + item + '</li>' for item in x)

If you want to get really crazy, this might be the most efficient (although I don't recommend it):

list_output = '<li>' + '</li>\n<li>'.join(item for item in x) + '</li>\n'

Upvotes: 3

recursive
recursive

Reputation: 86134

list_output = "<li>%s</li>\n" * len(x) % tuple(x)

Upvotes: 1

fixermark
fixermark

Reputation: 1260

Replace your for loop at the bottom with the following:

list_output=""
for aLine in x:
    list_output += '<li>'+aLine+'</li>\n'

Note also that since x is a list, Python lets you iterate through the elements of the list instead of having to iterate on an index variable that is then used to lookup elements in the list.

Upvotes: 0

Robert Rossney
Robert Rossney

Reputation: 96850

s = "\n".join(['<li>' + i + '</li>' for i in x])

Upvotes: 2

Related Questions