🔪

String Split Methods

Oct 6, 2025

Overview

This lecture covers the split and rsplit string methods in Python, explaining their syntax, how they work, and key differences. Homework is assigned at the end.

Split Method

  • The split method divides a string into a list of substrings.
  • Syntax: string.split(separator, maxsplit).
  • separator: character(s) where the split occurs; defaults to whitespace if not provided.
  • maxsplit: the maximum number of splits; defaults to -1 (no limit).
  • Example: "Hello!$I$am$Just Breathe".split("$", 2) results in ['Hello!', 'I', 'am$Just Breathe'].
  • If no arguments are supplied, splits on whitespace with unlimited splits.

Rsplit Method

  • The rsplit method splits a string into a list, starting from the right.
  • Syntax: string.rsplit(separator, maxsplit).
  • separator and maxsplit have the same meaning as in split.
  • Example: "Hello!$I$am$Just Breathe".rsplit("$", 2) results in ['Hello!$I', 'am', 'Just Breathe'].
  • Difference: split splits from the left; rsplit splits from the right.

Key Terms & Definitions

  • split — A method to divide a string into a list from the left using a separator.
  • rsplit — A method to divide a string into a list from the right using a separator.
  • separator — Character at which splits are performed.
  • maxsplit — Maximum number of splits allowed.

Action Items / Next Steps

  • Solve the homework: Determine the output of code using rsplit with no arguments.
  • Review examples to reinforce the difference between split and rsplit.