Parsing text (analyzing structure and extracting substrings)

>>> the_string = 'This is text parsing'
>>> string_1 = the_string[0:7]
>>> string_2 = the_string[13:]
>>> string_1
'This is'
>>> string_2
'parsing'

Using the split() method to chop up a string into a list of substrings, around a delimiter string. By default it uses the
space as a delimiter.

>>> the_string = 'This is the split method in action'
>>> split_action = the_string.split()
>>> split_action
['This', 'is', 'the', 'split', 'method', 'in', 'action']

Using a comma as a delimiter.

>>> my_list = 'apples, bananas, grapes, pears'
>>> fruits = my_list.split(',')
>>> fruits
['apples', ' bananas', ' grapes', ' pears']

Leave a Reply