Lesson 1.3: Strings
During this lesson, you will learn the following:
String Basics (indexing, slicing, membership, iterating)
String Formatting (for print statements)
Working with String (text) data
String Operators
For strings, + is concatenation.
For strings, * is repitition.
Strings as Ordered Sequences
A string is a sequence of characters:
Each character in the string has a position, called the index.
We can access each character in the string by its index position, using square brackets .
In Python, index positions start their counting with 0 (zero), not 1.
Strings are immutable (they cannot be changed)
Forward indexing starts at position 0
Python support reverse indexing (starting at position -1)
You can "count down" from the end of a string using the index as follows:
Slicing Strings
Python supports a special operation called a "slice" (using the operator "😊 to select a subset of a sequence, in this case here, a subset of characters in a string. The general format of a slice is as follows:
This yields the sub-sequence:
starting with the character
up to, but not including, the character
Iterating Over Strings with a While Loop
To iterate over a string with a
while
loop, you leverage the string indexThe
len()
function is useful building forwhile
loops over strings:It returns the length of a sequence (not just strings!)
It can be used to control loops over the string
Some care is needed when using the length b/c counting is zero-based (remember OBOE!)
Iterating Over Strings with For Loop
A
for
loop iterates over each element in the sequence from start to end (the use of the index is implicit).You use a variable (in this case "char") to represent each element in the list during loop execution:
The in
Operator for Strings
The word
in
is a boolean operator that takes two strings and returnsTrue
if the first appears as a substring in the secondThis provides a very convenient way to check for membership in a sequence.
String Comparisons
For strings, we already know the +
operator concatenates, and ==
tests for equality.
It turns out that the relational operators < and > have been implemented to reflect lexicographic ordering (i.e., indicate whether something comes earlier or later in a dictionary).
String Formatting in Python
This notebook covers the "old style" of string formatting (using printf-like syntax). This style of formatting is documented here. Essentially we use the % operator:
- when applied to integers, % is the modulus operator
- when the first operand is a string, % is the format operator
Details for the "new style" are covered in the Python 3 Documentation here. Although more powerful, this style is also more object-oriented (not covered here).