Remember
splitlines() splits at recognized line boundaries. By default the boundary characters are removed, and a final boundary does not add an empty last line.
splitlines() splits at recognized line boundaries. By default the boundary characters are removed, and a final boundary does not add an empty last line.
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\nmilk'
result = text.splitlines()
print(result)Top-level scope
'tea\nmilk'Object 2 · list['tea', 'milk']splitlines() splits at recognized line boundaries. By default the boundary characters are removed, and a final boundary does not add an empty last line.
(no output)
What do you expect this program to print?
EXAMPLE INSTRUCTIONS
text = 'tea\nmilk'
result = text.splitlines()
print(result)
|
v
DISPLAYED RESULT
['tea', 'milk']
FINAL BINDINGS
text --> object 1 (str)
result --> object 2 (list)
object 1: 'tea\nmilk'
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\nmilk'
result = text.splitlines()
print(result)Top-level scope
'tea\nmilk'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\nmilk'
result = text.splitlines()
print(result)splitlines() MethodThe splitlines() method in Python is used to split a string into a list of lines. It's especially useful when you're working with multiline strings, files, or text blocks where line breaks matter.
string.splitlines(keepends=False)
keepends (optional) – If True, line break characters (\n, \r, \r\n) are included in the output. Default is False.list of lines in the string.text = "Hello\nWorld\nPython"
lines = text.splitlines()
print(lines)
['Hello', 'World', 'Python']
text = "Hello\nWorld\nPython"
lines = text.splitlines(keepends=True)
print(lines)
['Hello\n', 'World\n', 'Python']
When reading file content into a single string, you can use splitlines() to process each line easily.
with open("file.txt", "r") as file:
content = file.read()
lines = content.splitlines()
for line in lines:
print(line)
splitlines() automatically handles:
\n – Unix/Linux line break\r\n – Windows line break\r – Old Mac line breaksplit('\n') instead of splitlines() will miss other newline types like \r and \r\n.keepends=True when line endings are important (e.g., in formatting).Use splitlines() in coding questions where you need to parse multi-line input efficiently. It’s cleaner than splitting manually with split('\n').
splitlines() splits a string into lines based on all common line break characters.keepends=True to retain line break characters.Write a program that reads a multi-line input from the user and prints each line with its line number.
text = """Python\nIs\nAwesome"""
for i, line in enumerate(text.splitlines(), 1):
print(f"Line {i}: {line}")
Connect what you noticed in the example with the idea below.
splitlines() splits at recognized line boundaries. By default the boundary characters are removed, and a final boundary does not add an empty last line.
A final newline always creates an extra empty field in splitlines().
This is the misconception to avoid.Reference checks are optional and do not count toward course progress.