brown wooden blocks on white surface
Photo by Brett Jordan on Unsplash

You have probably seen cases when you can do splits of strings into multiple parts based on a pattern that is observed in the string. For example, let us say that we want to save all the words that are part of a sentence. 

To do that, we usually try to do split the sentence into words based on the spaces that we find, such as:

Another example of this way of turning a string into a list of strings can be when doing the splitting using commas:

This is something quite known and widespread.

What is not that much known is the fact that the method split() can actually take a second argument that indicates the number of occurrences after which the splitting should stop.

For example, let us say that in the previous example, we want only to do the split for the first 2 occurrences of the comma and then stop. To do that, we simply need to specify the number 2 as follows:

This is probably not the best example to illustrate its usefulness, but I believe it should help you understand that such a quick solution actually exists.

I hope you find this useful.