Strings

8. Strings#

Hide code cell content

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

Strings are not like integers, floats, or booleans. A string is a sequence, which means it contains multiple values in a particular order.

In this chapter, we’ll learn how to access the individual values that comprise a string and utilize functions that process strings.

We’ll also utilize regular expressions, a powerful tool for identifying patterns in a string and performing operations such as search and replace.

  • re.search(pattern, text)

  • | alternative patter(s)

  • ^

  • $

  • re.sub(pattern, text, line)

As an exercise, you’ll have a chance to apply these tools to perform text analysis, which should be inspirational in learning more about text-mining and eventually lead to language models.