Python split

Python split function breaks the string into a list, where each word is the list item. Split is the build-in function means you don’t have to import any libraries to use this function. Table of …

Python split

Python split function breaks the string into a list, where each word is the list item. Split is the build-in function means you don’t have to import any libraries to use this function.

Syntax:

String.split(separator, maxsplit)

It has two parameters or two main components of the split string function.

  • Separator
  • maxsplit

Separator: 

It is an optional parameter and is used to separate list characters. If you don’t add a separator, it will automatically add blank space.

Maxsplit :

It is also an optional parameter that use to show the maximum limit of the split list. If you don’t add max split then, it will automatically set to the -1.

Which means infinity. 

The split method returns the copy of the string that represents a list. It does not make any changes. It uses separators to separate lists.

For instance, you can use “,” as a separator.

Here, See the given example below:

We created a list from a string using the split function and used “,” as a separator.

friends = "joe, Peter, james, sam"
x = friends.split(",")
print(x)
Output:
Python split

Let’s see another example of split function without any separator.

x = "pythonsplit"
list_of_letters = list(x)
print(list_of_letters)
Output:
Python split

Conclusion:

Python split() is used to convert a string into a list. It simply breaks the string characters to make a list from it. It has two parameters separator and max split. Both are optional parameters. If you don’t specify the separator, it will add blank space by default, and if you don’t specify max split, it will set the maximum limit to infinity by default.

Suggested Read: Remove Brackets from list Python

Leave a Comment