Strings

9.1. Strings#

Hide code cell source

import sys
from pathlib import Path

current = Path.cwd()
for parent in [current, *current.parents]:
    if (parent / '_config.yml').exists():
        project_root = parent  # ← Add project root, not chapters
        break
else:
    project_root = Path.cwd().parent.parent

sys.path.insert(0, str(project_root))

from shared import thinkpython, diagram, jupyturtle
from shared.download import download

# Register as top-level modules so direct imports work in subsequent cells
sys.modules['thinkpython'] = thinkpython
sys.modules['diagram'] = diagram
sys.modules['jupyturtle'] = jupyturtle

A string is a sequence of characters enclosed in quotes. A character can be a letter (in almost any alphabet), a digit, a punctuation mark, or white space.

It should be noted that strings are immutable, meaning after creation, they cannot be modified or updated.

Strings are one of the most commonly used data types in Python, and Python provides a rich set of built-in operations and methods for working with them.

In this section we cover:

  • Creating strings

  • Indexing and slicing

  • Concatenation and repetition

  • Case methods

  • Searching and testing

  • Cleaning

  • Splitting and joining

  • String formatting

  • Type-checking methods

  • String comparison

  • Docstrings

  • Application