0

Does someone know what [44:] is doing in statement huck_finn_text.split('CHAPTER ')[44: ] and why 44?

    huck_finn_url = 'https://bitbucket.org/bakuzen/bsu-fds/raw/master/lec/huck_finn.txt'
    huck_finn_text = read_url(huck_finn_url)
    huck_finn_chapters = huck_finn_text.split('CHAPTER ')[44:]

    # Display the chapters of Huckleberry Finn in a table.
    Table().with_column('Chapters', huck_finn_chapters)

Output is enter image description here

hello1949
  • 1
  • 3
  • 1
    That is splitting the text at each occurrence of the word 'CHAPTER ', and saving the 44th to the last occurrence in the huck_finn_chapters variable. – I break things Jan 18 '21 at 21:21
  • Apparently, there are 44 instances of `CHAPTER ` in the document prior to the instance that actually indicates the first chapter. Perhaps the document contains its own table of contents, and this is skipping over it? I can't tell without looking at the actual document, but the URL in your code isn't working. – jasonharper Jan 18 '21 at 21:22
  • 1
    URL is updated now @jasonharper – hello1949 Jan 18 '21 at 21:26
  • Thank you @marcdtheking – hello1949 Jan 18 '21 at 21:31

4 Answers4

0

split method separate texts in the url with a character or a word that here is 'Chapter '.

You can read about it here: https://www.w3schools.com/python/ref_string_split.asp

0

It is called slicing.

In this example you're slicing a list:

huck_finn_chapters = huck_finn_text.split('CHAPTER ')[44:]

And you are slicing the list from 44 index to the end.

Look at this answer here: https://stackoverflow.com/a/509295/11101156

Jakub Szlaur
  • 1,852
  • 10
  • 39
0

str.split splits a string, so it finds all occurrences of 'CHAPTER ' (that is the letters "CHAPTER" followed by a space), and returns a list of all the text between them.

something[a:b] is the slice operation, it means to take all the items from number a to number b. For example, if a = [3, 4, 5, 6, 7] then a[2:4] == [5, 6]. You can omit the first number to take everything from the start, or omit the second number to take everything until the end. In this case, some_list[44:] means to skip the first 44 items, and return the rest.

Jasmijn
  • 9,370
  • 2
  • 29
  • 43
0

The split method will return a list, and the [44:] means return every item in the list after the 44th. For example:

myList = ["hello","hi","yo","whatsup"]
print(myList[2:])
#will return ['yo', 'whatsup']