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
inputto read a value and convert it to the correct type withint/float; - explain why
type(input(...))is alwaysstr, and what goes wrong if you skip the conversion.
In this lesson
- Strings as a sequence type
- Operator overloading on strings
- Reading input
Before you start
The full lesson, with worked examples and exercise solutions, is for members.
Read the full lessonNew to SelfMadeQuant.com? See membership options or browse the Python for Quantitative Finance syllabus.