Intro to Python: Building Strings Using Lists

In Programming, Python by timfanelli1 Comment

A while back I began posting an Intro to Python series, and for lack of time I haven’t posted anything in it for a while. Since I’ve started writing plugins for pyBlosxom, I’ve gotten to learn a lot of cool stuff about the language. I wanted to do a write up on what’s easily become my favorite feature thus far, the String join method.

String’s join method takes the contents of a list, and concatenates them together using the contents of the string as a delimeter. For instance, suppose I have a list containing “a”, “b”, and “c”, and I want to make the string “a, b, c”, this is accomplished easily using join:

abclist = ["a","b","c"]
abcstring = ", ".join( abclist )

While this seems very simple, it can also be very powerful. Let’s look at a more realistic example. Suppose I have a list of filenames in a directory, and I want to generate links to each file at the url I could easily generate HTML containing all the links by doing the following:

imagefiles = getImageFileNames()
imagelinks = "<br/>".join( [ "<a href=''>%s</a>" % (imgfile,imgfile) for imgfile in imagefiles ] )

Assuming my imagefiles list contains 3 filenames, “a.jpg”, “b.jpg” and “c.jpg”, which would result in the following HTML:

<a href=''>a.jpg</a><br/>
<a href=''>b.jpg</a><br/>
<a href=''>c.jpg</a>

There’s a little bit more going on in this example. Let’s take it apart to see how it works. We know that "<br/>".join( list ) will result in the contents of the list concatenated together, delimited by “<br/>”. The list in this case is generated by the statement:

[ "<a href=''>%s</a>" % (imgfile,imgfile) for imgfile in imagefiles ]

There’s two things going on here, string substitution and dynamic list generation. Python’s string substition is very similar to using C’s sprintf method. You embed formatting codes into a string, and then specify the arguments as a tuple preceded by a percent sign. A simpler example would be something like the following:

x = "This is %s." % ( "my string" )

Dynamic list generation is also a very powerful technique for building lists in a single statement. The general form of this is:

list = [ expr for var in iterable ]

expr can be any valid Python expression using var. For example, suppose I had a dict type, and I wanted to make a list of it’s values, I could do:

mydict = {} 
dictvalues = [ mydict[ x ] for x in mydict.keys() ]

(Ignoring the fact, of course, that I could simply have asked for mydict.values())

So getting back to the example at hand, our expr is a string substitution expression using variable imgfile for every imgfile in imagefiles.

One should be cautious though, as it’s very easy to write statements that are extremely cryptic. I’m guilty of this in the various plugins I’ve written for pyBlosxom – as I’ve tended to over use this feature, putting together statements like this:

taglinks = "<div id='relatedtags'>%s%s</div>" % ( "related tags: ", ", ".join( ['<a href="%s%s" rel="tag">%s</a>' % (config['tag_url'],tag,tag) for tag in related] ) )

Overall though, a very nice feature that makes Python a very elegant language to use.


  1. Pingback: Intro to Ruby: Hello World | Zero Byte, LLC

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.