Python str.split() function


In Python, the str.split() method is used to divide a string into a list of substrings based on a specified separator. By default, it splits the string at whitespace characters. This method is very useful for processing and analyzing text data.

Syntax

str.split(sep=None, maxsplit=-1)
  • sep (optional): The delimiter (separator) string that determines where the splits should occur. If not specified, the method splits at any whitespace and discards empty strings from the result.
  • maxsplit (optional): The maximum number of splits to perform. If specified, the resulting list will have at most maxsplit + 1 elements. The default value of -1 means "all occurrences."

Example Usage

  1. Basic usage without a separator:
text = "Hello, how are you?" result = text.split() print(result) # Output: ['Hello,', 'how', 'are', 'you?']
  1. Using a specific separator:
text = "apple,banana,cherry" result = text.split(",") print(result) # Output: ['apple', 'banana', 'cherry']
  1. Using maxsplit:
text = "one two three four five" result = text.split(" ", 2) # Only split at the first two spaces print(result) # Output: ['one', 'two', 'three four five']
  1. Handling multiple separators:
text = "one,two,,three,,four" result = text.split(",") print(result) # Output: ['one', 'two', '', 'three', '', 'four']
  1. Using whitespace as a separator:
text = " Hello World " result = text.split() # Splits on whitespace by default print(result) # Output: ['Hello', 'World']

Summary

  • Use str.split() to break a string into a list of substrings.
  • You can specify a delimiter to control how the string is split and limit the number of splits with maxsplit.
  • The method is versatile for different types of string processing tasks, particularly in data cleaning and preparation.