Code Logo

Backstage Cue List with splitlines()

Published at22 Apr 2026
Python String Handling Easy 0 views
Like0

A stage manager has pasted a cue sheet into a notes app, but the text is messy. Some lines contain real instructions, some are blank spacer lines, and some have extra spaces hanging at the beginning or end because the list was copied from different sources.

Your job is to turn that raw block of text into a clean Python list of cues. Every non-empty line should become one item in the output after trimming outside whitespace. Blank lines should be ignored completely.

Suppose the pasted script looks like " Lights down\n\nMusic up \nCurtain call ". The cleaned result should be ["Lights down", "Music up", "Curtain call"]. The same rule should work even when the text came from a system that uses different line endings. What matters is the logical line structure, not which newline style was used behind the scenes.

This means the task has two parts working together: break the block into lines, then keep only the meaningful ones after trimming. The answer should preserve the original top-to-bottom order of the real cues.

Example Input & Output

Example 1
Input
script = "Cue A\r\nCue B\r\n\r\nCue C"
Output
["Cue A", "Cue B", "Cue C"]
Explanation

Different line endings still produce the same clean list of cues.

Example 2
Input
script = " Lights down\n\nMusic up \nCurtain call "
Output
["Lights down", "Music up", "Curtain call"]
Explanation

Blank spacer lines are ignored and the remaining cues are trimmed.

Example 3
Input
script = " \n \n"
Output
[]
Explanation

A pasted block with only blank or whitespace-only lines should produce an empty list.

Algorithm Flow

Recommendation Algorithm Flow for Backstage Cue List with splitlines()
Recommendation Algorithm Flow for Backstage Cue List with splitlines()

Solution Approach

When a string represents lines of text rather than one continuous sentence, Python's splitlines() is usually the most fitting tool. It understands line boundaries directly, which is exactly what a pasted cue sheet is built from.

That matters more than it might seem at first. A simple split("\n") can work for many inputs, but splitlines() is designed for text that may have different newline conventions. It keeps the code closer to the actual job: treat the input as lines of text, not as one string with one specific separator.

Once you have the lines, the rest is cleanup. Each line should be trimmed with strip(), and any line that becomes empty after trimming should be skipped. A compact Python solution looks like this:

return [line.strip() for line in script.splitlines() if line.strip()]

Reading that expression from left to right matches the workflow nicely. First walk through the lines, then strip outside whitespace, then keep only the ones that still contain actual content.

The repeated line.strip() in the comprehension is acceptable for a small exercise like this, though a loop can make the steps more explicit if you prefer readability over compactness. In loop form, you would build an empty list, strip each line, and append it only when the stripped version is non-empty.

The key idea is that this is not word parsing and it is not sentence tokenization. The meaningful structure in the input is the line break, so the solution should begin from that structure. splitlines() makes that intent very clear, and combining it with strip() gives you the cleaned cue list in just a few lines of Python.

Best Answers

python - Approach 1
def collect_backstage_cues(script):
    return [line.strip() for line in script.splitlines() if line.strip()]