textwrap は覚えとこう
文字列を適当な長さで区切って diff を表示する - forest book のお題だけど、読み逃してる条件が無ければ textwrap で充分。
import textwrap def limit_characters(line, start, width): return textwrap.wrap(line[start:], width) def test_limit_characters_null(): test_line = "" assert [] == limit_characters(test_line, 0, 2) def test_limit_characters_max(): test_line = "12345" result = { 1: ["1", "2", "3", "4", "5"], 2: ["12", "34", "5"], 3: ["123", "45"], 4: ["1234", "5"], 5: ["12345"], 6: ["12345"], } for i in xrange(1, 6): r = result[i] assert r == limit_characters(test_line, 0, i) def main(): test_limit_characters_null() test_limit_characters_max() if __name__ == "__main__": main()