Splitting strings

We can then split strings up again using the split() method (which we've already encountered, but this time we're going to provide it's optional argument):

>>> colors.split(';')
['#45ff23', '#2321FA', '#1298A3', '#A32912']

The optional argument lets you specify the string — not just the character — on which to split the string. So, for example, you could parse a hasty breakfast order by splitting on the word 'and':

>>> 'eggsandbaconandspam'.split('and')
['eggs', 'bacon', 'spam']

 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset