Split a string into a list of strings

split.py

We hold these truths to be self-evident,
that all men are created equal,
that they are endowed by their Creator with certain unalienable Rights,
that among these are Life, Liberty, and the pursuit of Happiness.

1 We hold these truths to be self-evident
2 that all men are created equal
3 that they are endowed by their Creator with certain unalienable Rights
4 that among these are Life, Liberty, and the pursuit of Happiness.

1 We
2 hold
3 these
4 truths
5 to
6 be
7 self-evident,
8 that
9 all
10 men
11 are
12 created
13 equal,
14 that
15 they
16 are
17 endowed
18 by
19 their
20 Creator
21 with
22 certain
23 unalienable
24 Rights,
25 that
26 among
27 these
28 are
29 Life,
30 Liberty,
31 and
32 the
33 pursuit
34 of
35 Happiness.

Things to try

  1. Measure the length of preamble, phrases, and words. If you give a str to len, len will tell you the number of characters in the str. If you give a list to len, len will tell you the number of items in the list.
    print(f"len(preamble) = {len(preamble)}") #preamble is a string of 211 characters.
    print(f"len(phrases) = {len(phrases)}")   #phrases is a list of 4 strings.
    print(f"len(words) = {len(words)}")       #words is a list of 35 strings.
    
    len(preamble) = 211
    len(phrases) = 4
    len(words) = 35
    

  2. Split the preamble into a list of characters, i.e., into a list of one-character strings.
    preamble = """\
    We hold these truths to be self-evident,
    that all men are created equal,
    that they are endowed by their Creator with certain unalienable Rights,
    that among these are Life, Liberty, and the pursuit of Happiness.
    """
    
    characters = list(preamble)   #characters is a list of 211 strings, each of one character.
    print(f"len(characters) = {len(characters)}")
    
    for i, c in enumerate(characters, start = 1):
        print(i, c)
    
    len(characters) = 211
    1 W
    2 e
    3
    4 h
    5 o
    6 l
    7 d
    etc.
    207 e
    208 s
    209 s
    210 .
    211
    
    
    Why is the last line of the above output empty?
    for i, c in enumerate(characters, start = 1):
        if c.isprintable():
            print(i, c)
        else:
            print(i, repr(c))   #printable representation of the character
    
    207 e
    208 s
    209 s
    210 .
    211 '\n'
    
    Why would we want to split a string into a list of characters? See Tweepy.

  3. Other ways to split a string into a list of strings are in Split.