Skip to content
All library documents

Python String Operations: Slicing, Searching, and Replacement

PDF Quant course library

Summary

This tutorial introduces Python strings as immutable sequences of characters and explains common operations for working with them. It covers concatenation, length, indexing and slicing, splitting text into lists, and searching with methods such as count, find, and index. It also describes prefix and suffix checks, membership tests, changing letter case, trimming whitespace or selected edge characters, formatting, replacement, and repetition.

Examples demonstrate typical inputs and outputs, including why concatenating a string directly with an integer raises a type error and how conversion allows the operation. The notes also point out that assigning one variable to another refers to the same string object. This is a general programming reference rather than a trading method; it does not discuss market data workflows, quantify performance, or compare alternative approaches such as more efficient ways to build strings.

Key ideas

  • Python strings are immutable, so operations that appear to modify text produce a new string.
  • Concatenation requires string operands, so other data types must first be converted.
  • Indexing and slicing select characters or ranges, while splitting turns text into a list.
  • Search methods differ in their behavior when a match is absent: some return a sentinel and index raises an exception.
  • Case conversion, trimming, formatting, replacement, and repetition cover common text transformations.

Tags

From a private course collection; the original is not published.