Jed Rembold
Sept 17, 2025
How could you represent the number of items shown to the right in a binary representation?
== for
floating numeric comparisons! Rounding might result in unexpected
falsehoods
0.1 + 0.1 + 0.1 != 0.3'I am a string'"I am also a string!""I'm sad you've gone"chr and
ordchr takes a base-10 integer and returns
the corresponding Unicode character as a string
chr(65) gives
"A" (capital A)chr(960) gives
"π" (Greek letter pi)ord goes the other direction, taking a
single character string and returning the corresponding base-10 integer
of that character in Unicode
ord("B") gives 66ord(" ") gives 32ord("π") gives 960The number of characters in a string is commonly called its length
The length of a string can be found using the build-in function
len()
>>> len("Totally awesome")
15In practiced, this function works for any
sequence, of which range is also an
example. I could also say len(range(5)) for
instance.
Concatenation is the act of taking two separate objects and bringing them together to create a single new object
For strings, concatenation takes the contents of one string and adds them to the end of another string
Python overloads the +
operator to concatenate sequences like strings
+ will add two numbers, but
will concatenate two strings!>>> 'fish' + 'sticks'
'fishsticks'Unlike in addition, order matters here!
>>> 'sticks' + 'fish'
'sticksfish'+)
in Python to concatenate strings\[5+5+5+5+5+5 = 6 \times 5\]
You multiply by a integer: the number of times you want the concatenation repeated
print("Betelguese, " * 3)That this works is just a cute instance of shared logic for this use-case, and does not extend further. You can not multiply two strings together, Python will not understand what you are trying to do
You can select individual characters from the string using the syntax
|||string|||[|||k|||]
where |||string||| is the variable
assigned to the desired string and |||k|||
is the index integer of the character you want
>>> print("spaghetti sauce"[5])
eA common use case is to grab the last character of the string, using
s[-1]
which is shorthand for
s[len(s)-1]Often, you may want more than a single character
Python allows you to specify a starting and an ending index through an operation known as slicing
The syntax looks like:
|||string|||[|||start||| : |||limit|||]
where |||start||| is the first index to
be included and everything up to but not including the
|||limit||| is included
|||start||| and
|||limit||| are actually optional (but the
: is not)
|||start||| omitted, the slice will
begin at the start of the string|||limit||| omitted, the slice will
proceed to the end of the stringCan add a third component to the slice syntax, called a stride
|||string|||[|||start||| : |||limit||| : |||stride|||]Specifies how large the steps are between each included index
Can also make the stride negative to proceed backwards through a string
>>> s = "spaghetti sauce"
>>> s[4:8]
hett
>>> s[10:]
sauce
>>> s[:10:2]
sahtiPython lets you use normal comparison operators to compare strings. For example,
|||string 1||| == |||string 2|||
is true if |||string 1||| and
|||string 2||| contain the same characters
in the same order
Comparisons involving greater than or less than are done similar to alphabetical ordering
All comparisons are done according to their Unicode values.
"cat" > "CAT"for loop!s = "hello"
for i in range(len(s)):
print(s[i], 'is letter', i)
s = "hello"
for letter in s:
print(letter)
Strings are what we call immutable: they can not be modified in place by clients.
You can “look” at different parts of the string, but you can not “change” those parts without making a whole new string
s = "Cats!"
s[0] = "R" # THIS WILL ERROR!!You can of course create a new string object with the desired traits:
s = "R" + s[1:]This applies to all methods that act on strings as well: they return a new string, they do not modify the original