Skip to content
Python for Quantitative Finance / Control Flow and Simple Numerical Programs

Strings and Formatted Input/Output

A ticker symbol, a CSV row read from a returns file, a user-typed strike price at a prompt — before any of these becomes a usable number or a lookup key, it passes through Python as a str. This note covers the basic operations every sequence type shares (length, indexing, slicing), the two overloaded operators (+ and *) that behave differently on strings than on numbers, and the input function together with the type-conversion step it always requires. The last point is a genuine, common bug: input always returns a string, even when the user types a number, and forgetting to convert it is a standing trap for anyone reading a strike price or a quantity from the keyboard.
Learning objectives. By the end of this note you should be able to:
  • index and slice a string correctly, including negative indices and omitted slice bounds;
  • explain why + and * behave differently on strings than on numbers (operator overloading);
  • use input to read a value and convert it to the correct type with int/float;
  • explain why type(input(...)) is always str, and what goes wrong if you skip the conversion.

In this lesson

  1. Strings as a sequence type
  2. Operator overloading on strings
  3. Reading input

Before you start

The full lesson, with worked examples and exercise solutions, is for members.

Read the full lesson

New to SelfMadeQuant.com? See membership options or browse the Python for Quantitative Finance syllabus.