ProceduralOCR J277
DashboardCoursePracticeReviewProgressGlossary
Saved on this deviceContinue learning

Procedural · OCR J277 programming practice

Progress stays in this browser unless you export it.

Course/Strings & arrays/Indexing and substrings
J277 · 2.2.3

Unit 8 · Lesson 2

Indexing and substrings

Lesson progress0 / 7

Build the idea

Start with the concept and its key vocabulary.

Key idea

Translate ERL substring, left and right operations into correct Python indexing and slicing.

Strings are zero-indexed. OCR ERL substring(start, count) takes a starting index and a number of characters. Python string[start:end] takes an exclusive end index instead. ERL left(n) and right(n) map to Python [:n] and [-n:].

Index

The numeric position used to locate an item, starting at zero.

Zero-indexed

Numbered so the first character or item has index 0.

Substring

A consecutive section extracted from a larger string.

Slice

Python syntax that extracts values from a start index up to, but not including, an end index.

Keep these in mind

  • The first character has index 0.
  • ERL substring's second argument is a character count, not an end index.
  • Python excludes the slice end index.
  • The last index of a non-empty string is its length minus one.

Learning objective

Translate ERL substring, left and right operations into correct Python indexing and slicing.

Exam tip

Write index numbers above the string before extracting a substring. Then distinguish ERL count from Python end position.

Common mistake

subject.substring(3,5) extracts five characters starting at index 3. Python subject[3:5] is not equivalent; the correct slice is subject[3:8].

PreviousNext