Duncan Booth: > def nsplit(s, sep, n): > return (s.split(sep) + [""]*n)[:n]
Another version, longer: from itertools import repeat def nsplit(text, sep, n): """ >>> nsplit("bcsn; 1000001; 1456", ";", 3) ['bcsn', ' 1000001', ' 1456'] >>> nsplit("bcsn; 1000001", ";", 3) ['bcsn', ' 1000001', ''] >>> nsplit("bcsn", ";", 3) ['bcsn', '', ''] >>> nsplit("", ".", 4) ['', '', '', ''] >>> nsplit("ab.ac.ad.ae", ".", 2) ['ab', 'ac', 'ad', 'ae'] """ result = text.split(sep) nparts = len(result) result.extend(repeat("", n-nparts)) return result if __name__ == "__main__": import doctest doctest.testmod() Bye, bearophile -- http://mail.python.org/mailman/listinfo/python-list