Remember
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.
Follow the example, change its sample, and compare what happens.
Explain the result and test your understanding.
Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.
Follow the code and watch its state change. Guided simulation.
text = ' tea milk '
result = text.split()
print(result)Top-level scope
' tea milk 'Object 2 · list['tea', 'milk']split() returns a list of fields. Without a separator it collapses whitespace and omits empty fields at the ends.
(no output)
What do you expect this program to print?
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']Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.
Choose a sample and follow its execution. Guided simulation.
text = ' tea milk '
result = text.split()
print(result)Top-level scope
' tea milk 'Object 2 · list['tea', 'milk']The example has finished. Compare the final output with the values and references above.
['tea', 'milk']
What do you expect this program to print?
text = ' tea milk '
result = text.split()
print(result)split() MethodThe 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.
string.split(separator, maxsplit)
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).text = "apple banana cherry"
result = text.split()
print(result)
['apple', 'banana', 'cherry']
Why? Because the default separator is whitespace.
data = "red,green,blue"
colors = data.split(",")
print(colors)
['red', 'green', 'blue']
line = "one two three four"
print(line.split(" ", 2))
['one', 'two', 'three four']
Why? It splits only the first two spaces.
[] when split on whitespace.In coding interviews, split() is commonly used for problems involving strings, word frequency, or text manipulation.
split() divides a string into a list.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)
Connect what you noticed in the example with the idea below.
split() returns a list of fields. Without a separator it collapses whitespace and omits empty fields at the ends.
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.