You are currently viewing Python split() method to split the string

Python split() method to split the string

The split() is a built-in method in Python and is used to split a string into substrings (words) based on a separator or delimiter which it takes as an argument. It returns a list containing the substrings separated by a specified delimiter(separator).

In simple terms, you have a sentence and you want to split it into words making them different entities, you can use the split() method and it will return a list containing the words.

The above code will split the string stored in the sentence variable into three words within a list.

You may be wondering how the string got separated when there is no delimiter specified as an argument.

Syntax

.split(sep=None, maxsplit=-1)

sep – If sep is not specified, the whitespace character is used as the delimiter otherwise, the string will be split based on the specified sep.

maxsplit – If maxsplit is not specified, the string will split until it reaches the end, creating as many elements as possible. Otherwise, if maxsplit is specified, the string will be split into maxsplit + 1 elements. For example, if you’ve specified maxsplit=1, then the string will split into 2 elements.

Splitting based on delimiter

Suppose you have a comma-separated sentence and you want to split that sentence into a list of substrings based on the comma, you can simply pass the separator as a comma (",") and be done.

If you run this code, you’ll get the following output.

You can see that the split(sep=",") split the sentence where the comma is placed and you get five substrings within a list.

You can split the string based on any delimiter such as commas, spaces, tabs, semicolons, etc., depending on the specific requirements of the data being processed.

Suppose you have the data containing names separated by a weird expression and you want to take out the names from the data.

This will work as fine as the code from the previous and you’ll get the list of names from the data and you can perform whatever operation you want.

Using maxsplit

If you have a specific number in mind for how your string should be split, you can use the maxsplit to specify that number.

In the above code, the maxsplit is set to 3 which means the string will be split into four substrings.

The string got split into four substrings instead of three despite setting the maxsplit to 3. This happened because, by default, one is added to the number of maxsplit, which means maxsplit=3 is equivalent to maxsplit=3 + 1, resulting in a maximum of 4 parts after splitting.

Example

Assume you have a file containing a person’s information (name, age, and email address), and you want to access the data from the file and perform some operations on it.

The function, get_details, reads a file given by the filename parameter. It then reads all the lines from the file using file.readlines(). Each line is split into a list of strings using the split() method, which splits the line based on whitespace by default. This creates a list of lists, where each inner list contains the individual words from each line.

Finally, the function returns this list of lists, where each inner list represents the words from each line in the file.

You can use the object (details) to access information about a specific person.

This will print the information for the person on the third, first, and ninth indexes respectively.

Conclusion

The split() method is used to split a string into substrings within a list. You can also split your string based on a delimiter which you can pass as an argument to the split() method and you can control the splits by setting a value to the maxsplit parameter.

Resource: https://docs.python.org/3.3/library/stdtypes.html#str.split


πŸ†Other articles you might be interested in if you liked this one

βœ…Why if __name__ == β€˜__main__’ is used in Python programs?

βœ…Create a WebSocket server and client in Python.

βœ…How to use map() in Python?

βœ…How to use pytest to test your code in Python?

βœ…Serialize and deserialize Python objects using the pickle module.

βœ…Hash password using the bcrypt package in Python.


That’s all for now

Keep Coding✌✌