What is the difference between split() and slicing in Python?

Both split() function and slicing work on a String object. By using split() function, we can get the list of words from a String.

E.g. ‘a b c ‘.split() returns [‘a’, ‘b’, ‘c’]
Slicing is a way of getting substring from a String. It returns another String.

E.g. >>> ‘a b c'[2:3] returns b

Leave a Reply

Your email address will not be published. Required fields are marked *