Python String split() Method – Split Strings Easily

Python String split() Method

The split() method is used to divide a string into a list of words or substrings based on a specified separator.

It is one of the most useful methods for handling text and is commonly used when processing user input, files, or data formats like CSV.

Syntax

string.split(separator, maxsplit)

Parameters:

  • separator (optional) – The delimiter where the string should be split. Default is whitespace.
  • maxsplit (optional) – The maximum number of splits to do. Default is -1 (no limit).

Returns:

  • A list of substrings.

Example 1: Basic Split

text = "apple banana cherry"
result = text.split()
print(result)
['apple', 'banana', 'cherry']

Why? Because the default separator is whitespace.

Example 2: Using a Comma Separator

data = "red,green,blue"
colors = data.split(",")
print(colors)
['red', 'green', 'blue']

Example 3: Using maxsplit

line = "one two three four"
print(line.split(" ", 2))
['one', 'two', 'three four']

Why? It splits only the first two spaces.

Use Cases

  • Splitting sentences into words
  • Parsing CSV or other delimited data
  • Processing user input
  • Reading log files or text content line-by-line

Common Mistakes

  • If the separator doesn’t exist in the string, the entire string is returned as one element in a list.
  • Empty strings still return [] when split on whitespace.

Interview Tip

In coding interviews, split() is commonly used for problems involving strings, word frequency, or text manipulation.

Summary

  • split() divides a string into a list.
  • Default separator is whitespace.
  • Returns a list of substrings.

Practice Problem

Write a program to read a sentence from the user and print all words separately.

sentence = input("Enter a sentence: ")
words = sentence.split()
print("Words:", words)