Python Reference Library →

Python String split() Method – Split Strings Easily

Let’s look at string split() method – split strings easily

  1. Notice

    split() returns a list of fields. Without a separator it collapses whitespace and omits empty fields at the ends.

  2. Try

    Follow the example, change its sample, and compare what happens.

  3. Check

    Explain the result and test your understanding.

Watch how it works

Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.

Explore the idea

Follow the code and watch its state change. Guided simulation.

Code
Python
text = ' tea  milk '
result = text.split()
print(result)
Live state

Top-level scope

text→Object 1
result→Object 2
Object 1 · str' tea milk 'Object 2 · list['tea', 'milk']
This step

split() returns a list of fields. Without a separator it collapses whitespace and omits empty fields at the ends.

Picture string split() method – split strings easily

split() returns a list of fields. Without a separator it collapses whitespace and omits empty fields at the ends.

Image brief

Alt text
A diagram for Python String split() Method – Split Strings Easily. Instructions: text = ' tea milk ' result = text.split() print(result). Displayed result: ['tea', 'milk']. The final example bindings are text --> object 1 (str); result --> object 2 (list). object 1: ' tea milk '; object 2: ['tea', 'milk']
Image generation prompt
Create a clean educational illustration for a Python programming lesson titled "Python String split() Method – Split Strings Easily". Teaching focus: split() returns a list of fields. Without a separator it collapses whitespace and omits empty fields at the ends. Depict exactly the relationships and labels in this reference diagram: EXAMPLE INSTRUCTIONS text = ' tea milk ' result = text.split() print(result) | v DISPLAYED RESULT ['tea', 'milk'] FINAL BINDINGS text --> object 1 (str) result --> object 2 (list) object 1: ' tea milk ' object 2: ['tea', 'milk'] Use a flat vector style, generous spacing, clear arrows and legible labels, with a restrained blue/teal palette on a plain background. The diagram must explain the programming idea accurately. Do not depict variables as boxes containing copied objects when they share a reference. Avoid mascots, decorative screenshots, logos, gradients and extra text. Landscape composition, approximately 3:2, suitable for a course card. Accessible description: A diagram for Python String split() Method – Split Strings Easily. Instructions: text = ' tea milk ' result = text.split() print(result). Displayed result: ['tea', 'milk']. The final example bindings are text --> object 1 (str); result --> object 2 (list). object 1: ' tea milk '; object 2: ['tea', 'milk']

Follow the execution

Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.

Execution workspace

Choose a sample and follow its execution. Guided simulation.

Code
Python
text = ' tea  milk '
result = text.split()
print(result)
Live state

Top-level scope

text→Object 1
result→Object 2
Object 1 · str' tea milk 'Object 2 · list['tea', 'milk']
This step

The example has finished. Compare the final output with the values and references above.

Terminal
Output
['tea', 'milk']

What do you expect this program to print?

What does this example display?

text = ' tea  milk '
result = text.split()
print(result)
What does this example display?
Choose one answer. You can try again.

Answer and explanation

['tea', 'milk']

Follow the printed values in order. The output is: ['tea', 'milk']

Which explanation matches what you learned?

Which explanation matches what you learned?
Choose one answer. You can try again.

Answer and explanation

split() returns a list of fields. Without a separator it collapses whitespace and omits empty fields at the ends.

split() returns a list of fields. Without a separator it collapses whitespace and omits empty fields at the ends.

More examples and reference

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)

What to remember

Connect what you noticed in the example with the idea below.

Remember

split() returns a list of fields. Without a separator it collapses whitespace and omits empty fields at the ends.

Watch out for this idea

split() with no separator preserves leading empty fields.

This is the misconception to avoid.

Reference checks are optional and do not count toward course progress.

    Reference library →