how to prepend string to a string?

Peter Hansen peter at engcorp.com
Mon May 30 21:39:57 EDT 2005


flamesrock wrote:
> so I know you can append a string. But how do you *prepend* a string,
> as shown in the following code
> 
> #dirList = ['depth1','depth2','depth3']
> #string = """position"""
> #for x in len(dirList):
> #   string += '>> %s'%dirList.pop()    #(????????????)

There's not _really_ anything wrong with this code, provided the length 
of dirList is "short" (since otherwise the performance issues of growing 
strings with += may cause trouble).  It might also be preferable to 
change from the for statement to a "while dirList:" since that doesn't 
require the unused "x" and the confusion of apparently testing the 
length of a list that is getting shorter with each iteration.

-Peter



More information about the Python-list mailing list