Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It sounds like you're just trying to add a string to a list of strings. That's just <code>append</code>:</p> <pre><code>&gt;&gt;&gt; inside = ['thing', 'other thing'] &gt;&gt;&gt; inside.append('another thing') &gt;&gt;&gt; inside ['thing', 'other thing', 'another thing'] </code></pre> <p>There's nothing specific here to strings; the same thing works for a list of <code>Item</code> instances, or a list of lists of lists of strings, or a list of 37 different things of 37 different types.</p> <p>In general, <code>append</code> is the most efficient way to concatenate a single thing onto the end of a list. If you want to concatenate a bunch of things, and you already have them in a list (or iterator or other sequence), instead of doing them one at a time, use <code>extend</code> to do them all at once, or just <code>+=</code> instead (which means the same thing as <code>extend</code> for lists):</p> <pre><code>&gt;&gt;&gt; inside = ['thing', 'other thing'] &gt;&gt;&gt; in_hand = ['sword', 'lamp'] &gt;&gt;&gt; inside += in_hand &gt;&gt;&gt; inside ['thing', 'other thing', 'sword', 'lamp'] </code></pre> <p>If you want to later concatenate that list of strings into a single string, that's the <code>join</code> method, as RocketDonkey explains:</p> <pre><code>&gt;&gt;&gt; ', '.join(inside) 'thing, other thing, another thing' </code></pre> <p>I'm guessing you want to get a little fancier and put an "and" between the last to things, skip the commas if there are fewer than three, etc. But if you know how to slice a list and how to use <code>join</code>, I think that can be left as an exercise for the reader.</p> <p>If you're trying to go the other way around and concatenate a list to a string, you need to turn that list into a string in some way. You can just use <code>str</code>, but often that won't give you what you want, and you'll want something like the <code>join</code> example above.</p> <p>At any rate, once you have the string, you can just add it to the other string:</p> <pre><code>&gt;&gt;&gt; 'Inside = ' + str(inside) "Inside = ['thing', 'other thing', 'sword', 'lamp']" &gt;&gt;&gt; 'Inside = ' + ', '.join(inside) 'Inside = thing, other thing, another thing' </code></pre> <p>If you have a list of things that aren't strings and want to add them to the string, you have to decide on the appropriate string representation for those things (unless you're happy with <code>repr</code>):</p> <pre><code>&gt;&gt;&gt; class Item(object): ... def __init__(self, desc): ... self.desc = desc ... def __repr__(self): ... return 'Item(' + repr(self.desc) + ')' ... def __repr__(self): ... return self.desc ... &gt;&gt;&gt; inside = [Item('thing'), Item('other thing')] &gt;&gt;&gt; 'Inside = ' + repr(inside) ... "Inside = [Item('thing'), Item('other thing')]" &gt;&gt;&gt; 'Inside = ' + str(inside) ... "Inside = [Item('thing'), Item('other thing')]" &gt;&gt;&gt; 'Inside = ' + ', '.join(str(i) for i in inside) ... 'Inside = thing, other thing' </code></pre> <p>Notice that just calling <code>str</code> on a list of <code>Item</code>s calls <code>repr</code> on the individual items; if you want to call <code>str</code> on them, you have to do it explicitly; that's what the <code>str(i) for i in inside</code> part is for.</p> <p>Putting it all together:</p> <pre><code>class Backpack: def __init__(self): self.inside = [] def add(self, toadd): self.inside.append(toadd) def addmany(self, listtoadd): self.inside += listtoadd def __str__(self): return ', '.join(str(i) for i in self.inside) pack = Backpack() pack.add('thing') pack.add('other thing') pack.add('another thing') print 'Your backpack contains:', pack </code></pre> <p>When you run this, it will print:</p> <pre><code>Your backpack contains: thing, other thing, another thing </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload