Python: split() function
split() function returns a list of strings after dividing the string based on the given separator (white space works as a separator) and it breaks down a large string into smaller chunks or strings.
In another way, we can say that it is the opposite of concatenation.Concatenation adds two strings.
Example-1:
Example-2:
Example-3:
[The max parameter in the above example is set to 2, which means the output will have 3 elements in the list of strings.]
In the above code snippet, we can see that, the split() function separates when it takes a comma, but this function does this only 2 times. Because, we provide the number of times that how many times it will separate or how many commas are counted as separator.
We can also split() function to take multiple input in a single line.
In the above code, we can see that we can take multiple input in a single line using split() function. And we also know that here all of the inputs are string as python take input as a string. That’s why type of a, b, c is string.
Note: As we don’t provide anything in the split() function, so here we have to take input using a space.
But, we can also take input using other separators such as comma.
We can take our desired input(int, float etc) using split() function in a single line. But we have to use another function map(). Let’s see the example:
Note: map() is a built-in function in python. It is also used to iterate a list, tuple etc.