Skip to content

Computer Science · Ch 8 — Strings

Introduction

8.1

Introduction

A sequence in Python is an ordered collection of items in which every item sits at a definite position identified by an integer, called its index. Chapter 5 introduced the three sequence data types that Python provides:

  • Strings
  • Lists
  • Tuples

Chapter 5 also introduced a fourth compound data type, the dictionary — but a dictionary is not a sequence. It belongs to a different category called mapping, because it associates keys with values rather than arranging items by position.

This chapter takes up the first of these types — the string — in full detail: how strings are created, how individual characters are reached through positive and negative indexing, why strings cannot be modified once created, the operations Python permits on them (concatenation, repetition, membership testing and slicing), how to traverse a string with for and while loops, the built-in functions and methods available for string manipulation, and finally some complete user-defined-function programs that put all of this to work.

The remaining compound types are covered in the chapters that follow: lists in Chapter 9, and tuples and dictionaries in Chapter 10.