Entries tagged “strings”

Joining strings in Python

written by Domen Kožar, on Jun 11, 2009 2:58:00 PM.

There is already a lot of good material to read about performance. Therefore, it is best to use the following method:
    >>> print ' '.join(['The', 'fox', 'jumped', 'over', 'the', 'dog.'])
    The fox jumped over the dog.
Perfect. We have very fast string contatenation that even allows us to choose separator. Now, here is the problem:
    >>> print ', '.join(['apples', 'oranges', '', 'cocos'])
    apples, oranges, , cocos
So, if we dynamically generate string and we have a separator for a reason, we do not want this to happen. This is basically the idea:
    >>> print ', '.join(filter(None, ['apples', 'oranges', '', 'cocos']))
    apples, oranges, cocos
As you can see, filter(None, iterable) returns list of elements that evalue to True. This way we have somewhat a safe string contatenation. Real world example:
class StringBuffer:

	def __init__(self, sep=''):
		self.sep = sep
		self.output = list()

	def write(self, content):
		self.output.extend(content)

	def getvalue(self):
		return self.sep.join(filter(None, self.output))


>>> sb = StringBuffer(', ')
>>> sb.write(['apples'])
>>> sb.write(['', None])
>>> sb.write(['oranges', 'cocos'])
>>> print sb.getvalue()
apples, oranges, cocos